Last active
February 9, 2021 06:22
-
-
Save HoukasaurusRex/2a54bb797bacef20903219d54eb19c86 to your computer and use it in GitHub Desktop.
JavaScript Interview Questions
This file contains hidden or 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
// Google Interview Video: https://www.youtube.com/watch?v=XKu_SEDAykw | |
// Provided an array of numbers, | |
// find a pair of numbers that add up | |
// to a provided sum | |
// O(n) linear time complexity | |
const hasPairWithSum = (arr, sum) => { | |
const set = new Set(); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set | |
const len = arr.length; // Setting the arr.length to a variable here will prevent creating the variable for every iteration in the array | |
for (let i = 0; i < len; i++) { | |
if (set.has(arr[i])) { // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/has | |
return true; | |
} | |
set.add(sum - arr[i]); // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/add | |
} | |
return false; | |
} |
This file contains hidden or 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 32-bit signed integer, reverse digits of an integer. | |
Note: | |
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. | |
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. | |
*/ | |
const reverse = (int) => { | |
let intString = int.toString() | |
if (intString[0] === '-') { | |
intString = `${intString}-` | |
} | |
const reversedInt = parseInt(intString.split('').reverse().join(''), 10) | |
return (reversedInt > (2**31 - 1) || reversedInt < -(2**31)) | |
? 0 | |
: reversedInt | |
} | |
reverse(123) // 321 | |
reverse(1534236469) // 0 | |
reverse(-1223) // -3221 |
This file contains hidden or 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
/** | |
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. | |
Symbol Value | |
I 1 | |
V 5 | |
X 10 | |
L 50 | |
C 100 | |
D 500 | |
M 1000 | |
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II. | |
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: | |
I can be placed before V (5) and X (10) to make 4 and 9. | |
X can be placed before L (50) and C (100) to make 40 and 90. | |
C can be placed before D (500) and M (1000) to make 400 and 900. | |
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. | |
*/ | |
const romanToInt = (romanStr) => { | |
// filter param datatype | |
// filter unrecognized characters | |
const romanHash = { | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, | |
'D': 500, | |
'M': 1000 | |
} | |
return romanStr.split('').reduce((acc, roman, i, arr) => { | |
const int = romanHash[roman] | |
const nextInt = romanHash[arr[i + 1]] | |
if (nextInt > int) { | |
acc -= int | |
} else { | |
acc += int | |
} | |
return acc | |
}, 0) | |
} | |
romanToInt('MCMXCIV') // 1994 | |
// Solution details: https://leetcode.com/submissions/detail/375386949/ |
This file contains hidden or 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
class LinkedList { | |
constructor(value) { | |
this.head = { | |
value: value, | |
next: null | |
}; | |
this.tail = this.head; | |
this.length = 1; | |
} | |
append(value) { | |
const newNode = { | |
value: value, | |
next: null | |
} | |
console.log(newNode) | |
this.tail.next = newNode; | |
this.tail = newNode; | |
this.length++; | |
return this; | |
} | |
prepend(value) { | |
const newNode = { | |
value: value, | |
next: null | |
} | |
newNode.next = this.head; | |
this.head = newNode; | |
this.length++; | |
return this; | |
} | |
printList() { | |
const array = []; | |
let currentNode = this.head; | |
while(currentNode !== null){ | |
array.push(currentNode.value) | |
currentNode = currentNode.next | |
} | |
return array; | |
} | |
insert(index, value){ | |
//Check for proper parameters; | |
if(index >= this.length) { | |
console.log('yes') | |
return this.append(value); | |
} | |
const newNode = { | |
value: value, | |
next: null | |
} | |
const leader = this.traverseToIndex(index-1); | |
const holdingPointer = leader.next; | |
leader.next = newNode; | |
newNode.next = holdingPointer; | |
this.length++; | |
return this.printList(); | |
} | |
traverseToIndex(index) { | |
//Check parameters | |
let counter = 0; | |
let currentNode = this.head; | |
while(counter !== index){ | |
currentNode = currentNode.next; | |
counter++; | |
} | |
return currentNode; | |
} | |
remove(index) { | |
// Check Parameters | |
const leader = this.traverseToIndex(index-1); | |
const unwantedNode = leader.next; | |
leader.next = unwantedNode.next; | |
this.length--; | |
return this.printList(); | |
} | |
reverse() { | |
//Code Here | |
if (this.length <= 1) { | |
return this.printList() | |
} | |
let firstNode = this.head | |
this.tail = this.head | |
let secondNode = firstNode.next | |
while (secondNode) { | |
let tempNode = secondNode.next; | |
secondNode.next = firstNode | |
firstNode = secondNode | |
secondNode = tempNode | |
} | |
this.head.next = null; | |
this.head = firstNode; | |
return this.printList(); | |
} | |
} | |
let myLinkedList = new LinkedList(10); | |
myLinkedList.append(5) | |
myLinkedList.append(16) | |
myLinkedList.prepend(1) | |
myLinkedList.printList() | |
myLinkedList.insert(2, 99) | |
myLinkedList.insert(20, 88) | |
myLinkedList.printList() | |
myLinkedList.remove(2) | |
myLinkedList.reverse() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment