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 string and an array of integers representing indices, capitalize all letters at the given indices. | |
For example: | |
capitalize("abcdef",[1,2,5]) = "aBCdeF" | |
capitalize("abcdef",[1,2,5,100]) = "aBCdeF". There is no index 100. | |
*/ | |
function capitalize(s,arr){ | |
var output = s.split(''); |
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
/* | |
We want to convert a URL query string into a nested object. The query string will contain parameters that may or may not have embedded dots ('.'), and these dots will be used to break up the properties into the nested object. | |
input: user.name.firstname=Bob&user.name.lastname=Smith&user.favoritecolor=Light%20Blue | |
output: { | |
'user': { | |
'name': { | |
'firstname': 'Bob', | |
'lastname': 'Smith' | |
}, |
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
[{"id":"id-zcucxdjnql","detail":1},{"id":"id-bpxw5jnlwvj","detail":2},{"id":"id-ghu82cv1ae5","detail":3}] |
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 binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). | |
For example: | |
Given binary tree [3,9,20,null,null,15,7], | |
3 | |
/ \ | |
9 20 | |
/ \ | |
15 7 | |
return its zigzag level order traversal as: |
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
/*Design a data structure that supports all following operations in average O(1) time. | |
insert(val): Inserts an item val to the set if not already present. | |
remove(val): Removes an item val from the set if present. | |
getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned. | |
Example: | |
// Init an empty set. | |
RandomizedSet randomSet = new RandomizedSet(); |
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
/*There are two sorted arrays nums1 and nums2 of size m and n respectively. | |
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). | |
Example 1: | |
nums1 = [1, 3] | |
nums2 = [2] | |
The median is 2.0 | |
Example 2: |
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 sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. | |
For example, | |
Given 1->2->3->3->4->4->5, return 1->2->5. | |
Given 1->1->1->2->3, return 2->3.*/ | |
class Node{ | |
constructor(val, next){ | |
this.val = val | |
this.next = next |
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 linked list, swap every two adjacent nodes and return its head. | |
For example, | |
Given 1->2->3->4, you should return the list as 2->1->4->3. | |
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.*/ | |
class Node{ | |
constructor(val, next){ | |
this.val = val | |
this.next = next |
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
//check if a binary tree is BST or not. | |
class Tree{ | |
constructor(val){ | |
this.val = val | |
this.left = null | |
this.right = null | |
} | |
insertLeft(val){ |
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 binary tree check if it's symmetric or it is a mirror image of itself. | |
class Tree{ | |
constructor(val){ | |
this.val = val | |
this.left = null | |
this.right = null | |
} | |
insertLeft(val){ |