/javascript-things

JavaScript Data Structures and Algorithms, built-in objects, design-patterns and more

Primary LanguageJavaScriptMIT LicenseMIT

javascript-things

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

    • 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. 
      

    • You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S 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 in J and S 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 and J will consist of letters and have length at most 50.
      • The characters in J are distinct.

  • 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 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 and B = 99 -> 1 -> 8 -> 10, return the node with value 8.

    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.