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
13 changes: 12 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,15 @@
// 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 arr= str.split('');
for (let i=0; i<=arr.length - 1; i++) {
for (let si = i + 1; si <= arr.length - 1; si++) {
if (arr[i] === arr[si]) {
return false;
}
}
}

return true;
}
12 changes: 11 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,14 @@

// 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 {}
export default function checkPermutations(s1: string, s2: string): boolean {
if (s1.length !== s2.length) return false;
const chars: Record<string, number> = {}
for (let i=0; i<=s1.length - 1; i++) {

s1[i] in chars ? chars[s1[i]]++ : chars[s1[i]] = 1;
s2[i] in chars ? chars[s2[i]]-- : chars[s2[i]] = -1;

}
return !Object.values(chars).some(item => item !== 0);
}
6 changes: 6 additions & 0 deletions technical-fundamentals/coding/problems/03_urlify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@

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

return s1.split('').map(item => {
if (item === ' ') {
return '%20';
}
return item;
}).join('');
}
16 changes: 16 additions & 0 deletions technical-fundamentals/coding/problems/04_palindromePermutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,21 @@
// ```

export default function palindromePermutation (str: string): boolean {
const sanitized = str.toLowerCase().replaceAll(' ', '').split('');

const [s1, s2] = sanitized.length % 2 === 0 ?
[sanitized.slice(0, sanitized.length / 2), sanitized.slice(sanitized.length / 2, sanitized.length)] :
[sanitized.slice(0, (sanitized.length / 2) + 1), sanitized.slice(sanitized.length / 2, sanitized.length)]

const chars: Record<string, number> = {}
for (let i=0; i<=s1.length - 1; i++) {

s1[i] in chars ? chars[s1[i]]++ : chars[s1[i]] = 1;
s2[i] in chars ? chars[s2[i]]-- : chars[s2[i]] = -1;

}

return !Object.values(chars).some(item => item !== 0);


}
22 changes: 21 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,25 @@
// 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 (Math.abs(str1.length - str2.length) > 1 ) {
return false
}

let acc = 0;
for (let i1=0, i2=0; i1<str1.length, i2<str2.length; i1++, i2++) {
if (str1[i1] !== str2[i2]) {
if (str1.length < str2.length) {
i2++;
} else if (str1.length > str2.length) {
i2--;
}
acc++
}
if (acc>1) {
return false;
}
}


return true;
}
13 changes: 12 additions & 1 deletion technical-fundamentals/coding/problems/06_stringCompression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,16 @@
// You can assume the string has only uppercase and lowercase letters (a - z).

export default function stringCompression (str: string) : string {


let count = 1;
let result = '';
for (let i = 0; i <= str.length - 1; i++) {
if (str[i] === str[i+1]) {
count++;
} else {
result += str[i] + count;
count = 1;
}
}
return result.length >= str.length ? str : result;
}
15 changes: 15 additions & 0 deletions technical-fundamentals/coding/problems/07_rotateMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,19 @@ type Matrix = number[][]

export default function rotateMatrix (matrix: Matrix) {

const rotatedMatrix= [];
//move all cols from left to rows from top
for (let col = 0; col < matrix[0].length; col++) {
const cols: number[] = [];
for (let row = matrix.length - 1; row >= 0; row--) {
cols.push(matrix[row][col]);
}
rotatedMatrix.push(cols);
}
// now replace original matrix since the parameter is passed as reference
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
matrix[row][col] = rotatedMatrix[row][col];
}
}
}
23 changes: 23 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,27 @@ type Matrix = number[][]

export default function zeroMatrix (matrix: Matrix) {

const rows: number[] = [];
const cols: number[] = [];
for (let row = 0; row < matrix.length; row++) {
if (matrix[row].includes(0)) {
rows.push(row)
}
for (let col = 0; col < matrix[row].length; col++) {
if (matrix[row][col] === 0) {
if (!cols.includes(col)) {
cols.push(col)
}
}
}
}
for (let row of rows) {
matrix[row] = matrix[row].map(() => 0)
}
for (let col of cols) {
for (let row = 0; row < matrix.length; row++) {
matrix[row][col] = 0
}
}
return matrix;
}
4 changes: 4 additions & 0 deletions technical-fundamentals/coding/problems/09_stringRotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ function isSubstring(s1: string, s2: string): boolean {

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

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

return isSubstring(s1 + s1, s2);

}