Skip to content

Instantly share code, notes, and snippets.

@EliEladElrom
Last active January 10, 2021 17:06
Show Gist options
  • Save EliEladElrom/450ce67da8f7a9322b87dee5a269b212 to your computer and use it in GitHub Desktop.
Save EliEladElrom/450ce67da8f7a9322b87dee5a269b212 to your computer and use it in GitHub Desktop.
Portion of -- Essential ReactJS & TypeScript Interview Questions and Answers: Learn and be prepared to conduct ReactJS
// https://www.amazon.com/dp/B08SKSWY8Z
// STRING
// #1 - Erase character from string
'hello world'.slice(1) // ello world
'hello world'.replace('o', '@') // hell@ world
'hello world'.replace(/o/g, '@') // hell@ w@rld - regex g=global
// #2 - A String is a palindrome (left to right is equal to the right to left)
function palindrome(str: string) {
// lower case and clean special char regex W
var lowRegStr = str.toLowerCase().replace(/[\W_]/g, '')
var reverseStr = lowRegStr.split('').reverse().join('')
return reverseStr === lowRegStr
}
palindrome("A man, a plan, a canal. Panama") // true
// #3 - Convert to strings
parseInt('12') // string to integer
parseFloat('1.2') // string to float
Number('12') // string to a number
// #4 - Maximum occurring character in a given string javascript regex
let getMax = function (str: string) {
var max = 0,
maxChar = '';
str.split('').forEach(function(char: string){
if(str.split(char).length > max) {
max = str.split(char).length;
maxChar = char;
}
});
return maxChar;
};
getMax('hello world') // return 'l'
// #5 - Find the first none / repeating character in a string
function firstRepeatingCharacter(str: string) {
for (let i = 0; i < str.length; i++) {
if (str.indexOf(str.charAt(i)) !== str.lastIndexOf(str.charAt(i))) {
return str.charAt(i)
}
}
return 'no results found'
}
firstRepeatingCharacter('123455') // 5
function firstNoneRepeatingCharacter(str: string) {
for (let i = 0; i < str.length; i++) {
if (str.indexOf(str.charAt(i)) === str.lastIndexOf(str.charAt(i))) {
return str.charAt(i)
}
}
return 'no results found'
}
firstNoneRepeatingCharacter('123455') // 1
// #6 Split a string
('hello world').split(' ') // ['hello', 'world']
// #7 Print duplicate characters in a string
const text = 'abcda'.split('')
text.some( (v, i, a) => {
return a.lastIndexOf(v) !== i
}) // true
// #8 Check duplicate characters in a string isogram
'test'.split('').some((value,index, array) => {
return array.lastIndexOf(value) !== index;
}) // true
// ARRAY
// #9 Find missing value in an integer array
const a = [3]
const count = 5
let missing = []
for (let i = 1; i <= count; i++) {
if (a.indexOf(i) === -1) {
missing.push(i)
}
}
console.log(missing) // [1, 2, 4, 5]
// #10 largest and smallest number in an array
Math.max(...[1, 3, 10]) // 10
Math.min(...[1, 3, 10]) // 1
// #11 missing numbers in an array
const arr = [1, 2, 3, 5, 8]
const [min, max] = [Math.min(...arr), Math.max(...arr)]
let out = Array.from(Array(max-min), (v, i)=>i+min).filter(i=>!arr.includes(i))
// #12 reverse an array
[1, 2, 3].reverse() // [3, 2, 1]
// #13 duplicate numbers in an array
let numArray: number[] = [1, 1, 2, 2, 3]
numArray.filter((element, index, array) => array.indexOf(element) !== index) // [1, 2]
// #14 remove duplicates from an array
[...new Set([1, 1, 2, 2, 3])] // [1, 2, 3]
// #15 convert a byte array into a string
String.fromCharCode.apply(null, [102, 111, 111]) // "foo"
// SORTING
// #16 sort
const array = ['Banana', 'Orange', 'Apple', 'Mango']
array.sort() // ascending - ["Apple", "Banana", "Mango", "Orange"]
array.sort().reverse() // descending
const numArray = [3, 5, 2, 1]
numArray.sort() // [1, 2, 3, 5]
// #17 Bubble sort
function bubbleSort(array: number[]) {
const len = array.length;
const retArray = array;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len - i; j++) {
const a = array[j];
if (a !== array[-1]) {
const b = array[j + 1];
if (a > b) {
retArray[j] = b;
retArray[j + 1] = a;
}
}
}
}
return retArray;
}
bubbleSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// #18 insertion sort
const insertionSort = (array: number[]) => {
const { length } = array;
const retArray = array;
for (let i = 1; i < length; i++) {
const key = array[i];
let j = i - 1;
while (j >= 0 && array[j] > key) {
retArray[j + 1] = array[j];
j -= 1;
}
retArray[j + 1] = key;
}
return retArray;
};
insertionSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// #19 merge sort
function merge(left: number[], right: number[]) {
const resultArray: number[] = []
let leftIndex = 0
let rightIndex = 0
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
resultArray.push(left[leftIndex])
leftIndex+=1
} else {
resultArray.push(right[rightIndex])
rightIndex+=1
}
}
return resultArray
.concat(left.slice(leftIndex))
.concat(right.slice(rightIndex))
}
function mergeSort(unsortedArray: number[]): number[] {
if (unsortedArray.length <= 1) {
return unsortedArray;
}
const middle = Math.floor(unsortedArray.length / 2);
const left = unsortedArray.slice(0, middle);
const right = unsortedArray.slice(middle);
return merge(
mergeSort(left), mergeSort(right)
)
}
mergeSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// #20 iterative quicksort algorithm
const swap = (arr: number[], i: number, j: number) => {
const tmp = arr[i]
const retArr = arr
retArr[i] = arr[j]
retArr[j] = tmp
return retArr
};
const partition = (arr: number[], low: number, high: number) => {
let q = low; let i;
for (i = low; i < high; i++) {
if (arr[i] < arr[high]) {
swap(arr, i, q)
q += 1
}
}
swap(arr, i, q)
return q
};
const quickSort = (arr: number[], low: number, high: number) => {
if (low < high) {
const pivot = partition(arr, low, high)
quickSort(arr, low, pivot - 1)
quickSort(arr, pivot + 1, high)
return arr
}
return []
}
quickSort([9, 8, 7, 6, 5, 4, 3, 2, 1], 4, 9) // [9, 8, 7, 6, undefined, 1, 2, 3, 4, 5]
// SEARCH
// #21 term search
'Hello world'.search('lo') // return where found 3
// #22 binary search algorithm
function binarySearchIndex(array: number[], target: number, low = 0, high = array.length - 1): number {
if (low > high) {
return -1
}
const midPoint = Math.floor((low + high) / 2)
if (target < array[midPoint]) {
return binarySearchIndex(array, target, low, midPoint - 1)
} if (target > array[midPoint]) {
return binarySearchIndex(array, target, midPoint + 1, high)
}
return midPoint
}
binarySearchIndex([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 9) // 8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment