Category: Intermediate

Determine if a binary tree is a binary search tree

Binary tree and Binary search tree are defined as follows : Binary tree is a tree data structure in which each node has at most two child nodes. A binary search tree (BST) is...

Flatten binary tree to linked list

Problem description : Convert a given binary tree to a linked list of all the nodes at each depth (if you have a binary tree with depth D, you’ll have D linked lists). Logic...

Word break problem dynamic programming

Problem : Given a string and an array of words, find out if the input string can be bro­ken into a space-separated sequence of one or more words. For example, inputDict = [“I” ,...

Pascals triangle javascript code

Problem : Create a pascal’s triangle using javascript. More details about Pascal’s triangle pattern can be found here. Input: #Rows = 6 Output: Logic : Pascal’s triangle can be simulated using 2-D array While...

Generate all permutations of a given array using backtracking

A permutation is a rearrangement of the elements in a list. A string/array of length n has n! permutation. Input: An array // [‘A’, ‘B’, ‘C’] Output: [‘A’, ‘B’, ‘C’] [‘A’, ‘C’, ‘B’], [‘B’,...

Justify if a string consists of valid parentheses

You are given an array of strings. Each of these strings is made up of bracket characters only : ‘(‘, ‘)’, ‘{‘,  ‘}’,  ‘[‘,  ‘]’. Programming languages utilize these brackets in balanced pairs, so...

Find a pair of elements from an array whose sum equals a given number

Input: An array of n integers and given a number X. Expected output: All the unique pairs of elements (a, b), whose summation is equal to X. Example <pre>Given<span class=”pl-k”>var</span> unSortedArr <span class=”pl-k”>=</span> [<span...

Determine given a pattern and string are isomorphic

Problem : Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and...

Determine if given strings are isomorphic

Problem : Given two (dictionary) words as Strings, determine if they are isomorphic. Two words are called isomorphic if the letters in one word can be remapped to get the second word. Remapping a...

Find consecutive segments in an sorted array

Problem : Number list compressing. Given an sorted array. Input: sorted number list [ 1, 2, 3,10, 25, 26, 30, 31, 32, 33] Output: find consecutive segments print: 1-3, 10, 25-26, 30-33 Logic :...