This repository contains implementaions of data structures and algorithms, design-patterns, built-in javascript objects and solutions to the problems from various platforms like LeetCode, Daily Coding Problem and more.
- Data Structures and Algorithms
- Built-in JavaScript Objects and Utils
- Design Patterns
- Leet Code
- Daily Coding Problem
- Misc
-
-
Day 1 - First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Example:
Given n = 5, and version = 4 is the first bad version. call isBadVersion(3) -> false call isBadVersion(5) -> true call isBadVersion(4) -> true Then 4 is the first bad version.
-
Day 2 - Jewels and Stones
You're given strings
J
representing the types of stones that are jewels, andS
representing the stones you have. Each character inS
is a type of stone you have. You want to know how many of the stones you have are also jewels.The letters in
J
are guaranteed distinct, and all characters inJ
andS
are letters. Letters are case sensitive, so"a"
is considered a different type of stone from"A"
.Example 1:
Input: J = "aA", S = "aAAbbbb" Output: 3
Example 2:
Input: J = "z", S = "ZZ" Output: 0 Note:
S
andJ
will consist of letters and have length at most 50.- The characters in
J
are distinct.
-
-
This problem was recently asked by Google.
Given a list of numbers and a number k, return hether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, eturn true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
This problem was asked by Google.
A unival tree (which stands for "universal value") is a tree where all nodes under it have the same value.
Given the root to a binary tree, count the number of unival subtrees.
For example, the following tree has 5 unival subtrees:
0 / \ 1 0 / \ 1 0 / \ 1 1
This problem was asked by Twitter.
You run an e-commerce website and want to record the last
N order
ids in a log. Implement a data structure to accomplish this, with the following API:record(order_id): adds the order_id to the log
get_last(i): gets the ith last element from the log. i is guaranteed to be smaller than or equal to N.
You should be as efficient with time and space as possible.
Problem 20 This problem was asked by Google.
Given two singly linked lists that intersect at some point, find the intersecting node. The lists are non-cyclical.
For example, given
A = 3 -> 7 -> 8 -> 10
andB = 99 -> 1 -> 8 -> 10
, return the node with value8
.In this example, assume nodes with the same value are the exact same node objects.
Do this in O(M + N) time (where M and N are the lengths of the lists) and constant space.