Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion technical-fundamentals/coding/problems/01_isUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,39 @@
// Implement an algorithm to determine if a string has all unique characters.
// What if you cannot use additional data structures?

export default function isUnique(str: string): boolean {}
export default function isUnique(str: string): boolean {

const sorted = str.split("").sort().join("");
let prev = null;
for(let i = 0; i < sorted.length; i++) {
if(sorted[i] === prev) return false;
prev = sorted[i];
}
return true;

}

export function tsSimple(str: string){
const map: Record<string, boolean> = {};
for(let i = 0; i < str.length; i++) {
if(map[str[i]]) {
return false;
} else{
map[str[i]] = true;
}
}
return true;
}

const pureJSSimple = function(str){
const map = {};
for(let i = 0; i < str.length; i++) {
console.log(map[str[i]]);
if(map[str[i]] === true) {
return false;
} else{
map[str[i]] = true;
}
}
return true;
}
32 changes: 31 additions & 1 deletion technical-fundamentals/coding/problems/02_checkPermutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,34 @@

// Given two strings, write a method to decide if one is a permutation of the other.

export default function checkPermutations(s1: string, s2: string): boolean {}
//O(n) -better but with more space complexity-
export default function checkPermutations(s1: string, s2: string): boolean {
const letterSet: Set<string> = new Set();
const letterMap: Record<string, number> = {};

for (let i = 0; i < s1.length; i++) {
letterSet.add(s1[i]);
if(letterMap[s1[i]]) {
letterMap[s1[i]]++;
} else {
letterMap[s1[i]] = 1;
}
}

for (let i = 0; i < s2.length; i++) {
if(!letterMap[s2[i]] || letterMap[s2[i]] === 0) {
return false;
}
if(letterMap[s2[i]] === 1) {
letterSet.delete(s2[i]);
}
letterMap[s2[i]]--;
}

return letterSet.size === 0;
}

//simpler solution sorting O(nlogn)
export function checkSimple(s1: string, s2: string): boolean{
return s1.split('').sort().join('') === s2.split('').sort().join('');
}
25 changes: 24 additions & 1 deletion technical-fundamentals/coding/problems/03_urlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@
// You may assume that the string has sufficient space at the end to hold the additional characters,
// and that you are given the "true" length of the string.

export default function URLify (s1 : string): string {
export default function URLify (s1 : string[]): string[] {

let next = s1[s1.length - 1];
let index: number = s1.length - 1;
while(next === ' '){
index--;
next = s1[index];
}

let pointer = s1.length - 1;

for(let i = index; i >= 0; i--){
if(s1[i] !== ' '){
s1[pointer] = s1[i];
pointer--;
} else {
s1[pointer] = '0';
s1[pointer - 1] = '2';
s1[pointer - 2] = '%';
pointer -= 3;
}
}

return s1;

}
25 changes: 25 additions & 0 deletions technical-fundamentals/coding/problems/04_palindromePermutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,29 @@

export default function palindromePermutation (str: string): boolean {

//worst case time complexity = O(2n) -> O(n)

let charMap: Map<string, number> = new Map();
for (let i = 0; i < str.length; i++) {
if(str[i] === ' ') continue;
const currChar = str[i].toLowerCase();
if(charMap.has(currChar)){
charMap.set(currChar, charMap.get(currChar) + 1);
} else {
charMap.set(currChar, 1);
}
}

let oddValues: number = 0;

for(const [_, number] of charMap){
if(number % 2 !== 0 && oddValues > 0) {
return false;
} else if(number % 2 !== 0){
oddValues++;
}
}

return true;

}
25 changes: 24 additions & 1 deletion technical-fundamentals/coding/problems/05_oneAway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,28 @@
// Given two strings, write a function to check if they are one edit (or zero edits) away.

export default function isOneAway(str1: string, str2: string): boolean {


if(str1.length > str2.length + 2 || str1.length < str2.length - 2) return false;
if(str1 === str2) return true;

for(let i = 0; i < str1.length; i++){
if(str1[i] !== str2[i]){
return equalsWithDelete(str1.substring(i), str2.substring(i)) ||
equalsWithInsert(str1.substring(i), str2.substring(i)) ||
equalsWithReplace(str1.substring(i), str2.substring(i));
}
}

return true;

}

export function equalsWithDelete(str1: string, str2: string): boolean {
return str1.substring(1) === str2 || str1 === str2.substring(1);
}
export function equalsWithInsert(str1: string, str2: string): boolean {
return str2[0] + str1 === str2 || str1[0] + str2 === str1;
}
export function equalsWithReplace(str1: string, str2: string): boolean {
return str2[0] + str1.substring(1) === str2 || str1[0] + str2.substring(1) === str1;
}
17 changes: 17 additions & 0 deletions technical-fundamentals/coding/problems/06_stringCompression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,22 @@
// You can assume the string has only uppercase and lowercase letters (a - z).

export default function stringCompression (str: string) : string {
let counter: number = 0;
let prev: string = '';
let output: string = '';
for(let i: number = 0; i < str.length; i++){
if(str[i] !== prev){
if(prev != ''){
output += prev + counter.toString();
counter = 0;
}
prev = str[i];
}
counter++;
}

output += prev + counter.toString();

return output.length < str.length ? output : str;

}
14 changes: 13 additions & 1 deletion technical-fundamentals/coding/problems/07_rotateMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,16 @@ type Matrix = number[][]

export default function rotateMatrix (matrix: Matrix) {

}
for(let i = 0; i < matrix.length; i++){
for(let j = i; j < matrix.length; j++){
const aux = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = aux;
}
}

for(let i = 0; i < matrix.length; i++){
matrix[i].reverse();
}

}
25 changes: 25 additions & 0 deletions technical-fundamentals/coding/problems/08_zeroMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,29 @@ type Matrix = number[][]

export default function zeroMatrix (matrix: Matrix) {

let setRow: Set<number> = new Set();
let setCol: Set<number> = new Set();
for(let i = 0; i < matrix[0].length; i++){
for(let j= 0; j < matrix.length; j++){
if(matrix[i][j] === 0 && !setRow.has(i) && !setCol.has(j)){
fillRow(i, matrix[0].length, matrix);
fillCol(j, matrix.length, matrix);
setRow.add(i);
setCol.add(j);
}
}
}

}

export function fillCol(col:number, mlength: number, matrix: Matrix){
for(let i = 0; i < mlength; i++){
matrix[i][col] = 0;
}
}

export function fillRow(row:number, mlength: number, matrix: Matrix){
for(let j = 0; j < mlength; j++){
matrix[row][j] = 0;
}
}
22 changes: 21 additions & 1 deletion technical-fundamentals/coding/problems/09_stringRotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,24 @@ import { isSubstring } from "./__utils__/strings"
// Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring.
// [e.g., "waterbottle" is a rotation of 'erbottlewat")

export default function stringRotation(s1: string, s2: string): boolean {}
export default function stringRotation(s1: string, s2: string): boolean {

if(s1.length != s2.length) return false;

let stringEnd = "";

for(let i = 0; i < s1.length; i++){
if(s2[i] !== s1[0]){
stringEnd += s2[i];
} else {
if(s2.substring(i) + stringEnd === s1){
return true;
} else {
stringEnd += s2[i];
}
}
}

return false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ describe('02 - checkPermutation', () =>{
test('Returns false for long strings with different characters', () =>{
expect(checkPermutations('a'.repeat(1000),'b'.repeat(1000))).toEqual(false);
});

test('Returns false for non-permutations with subgroup in the second one -it prevents uncomplete solutions-', () =>{
expect(checkPermutations('abc','bc')).toEqual(false);
});
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import URLify from "../../03_urlify";

describe('03 - URLify', () =>{
test("Replaces spaces in a string with '%20'", () =>{
expect(URLify('ab c')).toEqual('ab%20c');
expect(URLify([...'ab c '])).toEqual([...'ab%20c']);
});

test("Handles leading and trailing spaces", () =>{
expect(URLify(' ab c ')).toEqual('%20%20ab%20c%20%20');
expect(URLify([...' ab c '])).toEqual([...'%20%20ab%20c']);
});

test("Returns empty string when input is empty", () =>{
expect(URLify('')).toEqual('');
});

test("Doesn't modify string without spaces", () =>{
expect(URLify('abc')).toEqual('abc');
expect(URLify([...'abc'])).toEqual([...'abc']);
});

test("Handles multiple consecutive spaces", () =>{
expect(URLify('a b c')).toEqual('a%20%20b%20%20%20c');
expect(URLify([...'a b c '])).toEqual([...'a%20%20b%20%20%20c']);
});

test("Handles special characters", () =>{
expect(URLify('a b!c')).toEqual('a%20b!c');
expect(URLify([...'a b!c '])).toEqual([...'a%20b!c']);
});

test("Mr 3ohn Smith", () =>{
expect(URLify('Mr 3ohn Smith')).toEqual('Mr%203ohn%20Smith');
expect(URLify([...'Mr 3ohn Smith '])).toEqual([...'Mr%203ohn%20Smith']);
});
});