This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple Binary search implementation in Js | |
const binarySearch = (arr, key) => { | |
if (arr == null || typeof arr === 'undefined') return null; | |
if (arr.length === 1) { | |
if (key === arr[0]) { | |
return arr[0]; | |
} else { | |
return 'not found'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const parenthesis = { | |
brace: ['(', ')'], | |
square: ['[', ']'], | |
curly: ['{', '}'] | |
}; | |
const parensKeys = Object.keys(parenthesis); | |
const isOpenParenthesis = (p) => { | |
for (let i = 0; i < parensKeys.length; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. | |
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6]. | |
*/ | |
const productArray = (arr) => { | |
const newArr = []; | |
let product = 1; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Given a list of numbers and a number k, return whether any two numbers from the list add up to k. | |
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17. | |
*/ | |
const arrayElementsSum = (arr, k) => { | |
// If k - index exists in array, return true | |
for (let i = 0; i < arr.length; i++) { | |
const diff = k - arr[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i. | |
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6]. | |
*/ | |
const productArray = (arr) => { | |
const newArr = []; | |
for (let i = 0; i < arr.length; i++) { | |
let product = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const findFirstMissingPositiveInteger = input => { | |
const sortedInput = input.sort((a, b) => { | |
if (a < b) return -1; | |
if (a > b) return 1; | |
return 0; | |
}); | |
const nthInput = input.length - 1; | |
if (sortedInput[nthInput] <= 0) return 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. | |
For example, given the following Node class | |
class Node: | |
def __init__(self, val, left=None, right=None): | |
self.val = val | |
self.left = left | |
self.right = right |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int diagonalDifference(List<List<Integer>> arr) { | |
int primarySum = 0; | |
int secondarySum = 0; | |
for (int i = 0; i < arr.size(); i++) { | |
primarySum += arr.get(i).get(j); | |
secondarySum += arr.get(i).get(arr.size() - j - 1); | |
} | |
return Math.abs(primarySum - secondarySum); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ds = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; | |
const getNumStringArr = (num) => { | |
const numArr = num.toString().split(''); | |
const resultArr = []; | |
for (let i = 0; i < numArr.length; i++) { | |
const m = parseInt(numArr[i]); | |
resultArr.push(ds[m]); | |
} | |
return resultArr; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ds2 = { | |
unit: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'], | |
tens: ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] | |
}; | |
const isUnit = (num) => num % 10 === parseInt(num); | |
const isTeen = (num) => num[0] === '1' && num.length > 1; | |
const isTens = (num) => num[0] > 1 && num.length > 1; | |
const getNumStrArr2 = (num) => { | |
const parts = num.toString().match(/.{1,2}/g); | |
const resultArr = []; |
OlderNewer