javascript-things
javascript-things copied to clipboard
JavaScript Data Structures and Algorithms, built-in objects, design-patterns and more
JavaScript Things
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.
Table of Contents
- Data Structures and Algorithms
- Built-in JavaScript Objects and Utils
- Design Patterns
- Leet Code
- May Leet Coding Challenge
- Daily Coding Problem
- Easy
- Medium
- Hard
- Misc
Data Structures and Algorithms
- Recursion
- Linked Lists
- Singly Linked List
- Doubly Linked List
- Problems
- Stacks
- Using Array
- Using Linked List
- Problems
- Trees
- Binary Search Tree
- Problems
- Graphs
- Adjacency List
Built-in JavaScript Objects and Utils
-
Standard built-in objects
- Array
- map
- filter
- reduce
- Function
- call
- apply
- bind
- Promise
- Object
- create
- Array
-
Utils
- Currying
- Debounce
- Throttle
- Deep Clone Object
- getElementsByClassName
- iterator
- Prototypal Inheritance
Design Patterns
- Constructor Pattern
- Module Pattern
- Singleton Pattern
- Observer Pattern
- PublishSubscribe Pattern
Leet Code
-
May Leet Coding Challenge
-
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
Jrepresenting the types of stones that are jewels, andSrepresenting the stones you have. Each character inSis a type of stone you have. You want to know how many of the stones you have are also jewels.The letters in
Jare guaranteed distinct, and all characters inJandSare letters. Letters are case sensitive, so"a"is considered a different type of stone from"A".Example 1:
Input: J = "aA", S = "aAAbbbb" Output: 3Example 2:
Input: J = "z", S = "ZZ" Output: 0 Note:SandJwill consist of letters and have length at most 50.- The characters in
Jare distinct.
-
Daily Coding Problem
-
Easy
Problem 1
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?
Problem 8
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
Problem 16
This problem was asked by Twitter.
You run an e-commerce website and want to record the last
N orderids 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 -> 10andB = 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.
