Skip to content

Instantly share code, notes, and snippets.

View Smakar20's full-sized avatar

Sohini Makar Smakar20

  • Rally Health Inc
  • San Francisco
View GitHub Profile
@Smakar20
Smakar20 / index.js
Created January 9, 2019 07:14
capitalize.js created by smakar20 - https://repl.it/@smakar20/capitalizejs
/*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('');
@Smakar20
Smakar20 / index.js
Created January 8, 2019 19:41
convertQueryToMap.js created by smakar20 - https://repl.it/@smakar20/convertQueryToMapjs
/*
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'
},
@Smakar20
Smakar20 / details.txt
Created May 17, 2018 17:07
serialize and deserialize a tree created by smakar20 - https://repl.it/@smakar20/serialize-and-deserialize-a-tree
[{"id":"id-zcucxdjnql","detail":1},{"id":"id-bpxw5jnlwvj","detail":2},{"id":"id-ghu82cv1ae5","detail":3}]
/*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:
/*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();
/*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:
@Smakar20
Smakar20 / deleteDuplicateNodes.js
Created December 30, 2017 08:38
deleteDuplicateNodes created by smakar20 - https://repl.it/@smakar20/deleteDuplicateNodes
/*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
@Smakar20
Smakar20 / swapPairsInLinkedList.js
Last active December 30, 2017 08:00
swapPairsInLinkedList created by smakar20 - https://repl.it/@smakar20/swapPairsInLinkedList
/*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
@Smakar20
Smakar20 / checkBST.js
Created December 29, 2017 18:54
checkBST created by smakar20 - https://repl.it/@smakar20/checkBST
//check if a binary tree is BST or not.
class Tree{
constructor(val){
this.val = val
this.left = null
this.right = null
}
insertLeft(val){
//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){