Build nested object from hierarchical list- code

build nested object

Similar to convert JSON file to objects, we can covert hierarchical list to a nested object. The input is like a table of content with multiple levels, the output is a nested object obj.children[1].children[2]. “obj” is the top level object. Each nested object’s level maps with the level in the …

Continue reading

British royal succession order – code

royal succession order

British royal succession order is also known as the line of succession to the British throne. Under common law, the crown is inherited by a sovereign’s children or by a childless sovereign’s nearest collateral line. You can get the current British monarchy succession order here. When you are asked to …

Continue reading

Word break using memoization – Code

wordbreak with memoization

Word break is to divide a string into sub-strings that defined in dictionary. The problem is usually solved with recursion. The time complexity is exponential. Here we introduce word break using memoization, which helps to improve complexity. Memoization is one of Dynamic programming(DP) techniques. DP is able to solve a …

Continue reading

Find a random number NOT in array – code

find random not in array

Find random number not in array is a tricky question. It is pretty straight forward to find a random element in array by getting an random index within the range. If you are asked to find a random element that is NOT in array, your brain might not response quickly …

Continue reading

Combinations of adding operators and parentheses

possible expressions

Given a set of numbers, there are many different ways to add operators and parentheses to form an valid expression. The operations are +, -, *, /. The operator * and / have higher precedence than + and – . In infix expressions, we add parentheses to override the normal …

Continue reading

Initialize game board in matrix – Code

initialize game board

Initialize game board in matrix is to populate one digit number into a MxN matrix. The digit can be generated randomly. However, no 3 adjacent cells shall be the same, horizontal, vertical, diagonal and L shape. If you find invalid digit, replace with a different one in that cell. Matrix …

Continue reading

Find all subset of string in dictionary – code

find all subset string

Find all subset of string in dictionary is to find the subset of an input string that exists in dictionary. The dictionary contains one million words. For this question, the trick is to find subset, not substring. The difference between substring and subset is: The substring is contiguous sequence of …

Continue reading