interview questions

PostOrder Node Iterator of Binary Tree

Introduction This is going to be a short post. The problem statement at hand is to write PostOrder Node Iterator of Binary Tree. For those who do not know the Iterator Design Pattern or Post Order Successor, check the next section. Iterator Design Pattern This pattern is used to iterate through a collection or group. The Java Iterator supports the following three methods : hasNext() : returns a boolean true if the collection has elements after the last returned element, false otherwise. next() : returns the next element in the collection. remove() : removes the element from the coll...
Read More

Minimum Range over K arrays

Introduction This is one interesting problem with a non-trivial solution, this has been asked in couple of good interviews. Here goes the problem statement. "Given K positive integer arrays, each of which contains elements in sorted order. Find a range of integers such that it must contain at least one element from each array and the range is minimum." May be a diagram can make this more clear. Here you go! Understanding the Minimum Range over K arrays problem. In the above diagram we have 3 arrays each containing elements in increasing order. Now, let us identify a range which cont...
Read More

Shortest path in Binary Search Tree

Introduction Another interview question for the most interesting data structure called BST (Binary Search Tree). To know more about BSTs check my previous post. Problem Statement : Given a Binary Search Tree, keyed on positive integers. The task is to find the Shortest path in Binary Search Tree which adds up to the number K. If no such path exists, return a message accordingly. Understanding the problem A node in a binary search tree always has a higher key at its right child compared to its left child. There are many path running from the roots to the leaves. When we add up the key val...
Read More

Dynamic Programming – Longest Palindromic Sequence

[nextpage title="Introduction"] Palindromes are fascinating character sequences in a string. A palindrome is a string which reads the same when read from either of the ends. This post in particular talks about palindromic sub sequences. To know more about a sub sequence, please check my post on Longest Common Sub sequence. A palindromic sequence means a sequence of characters which is a palindrome. Now, we must understand it clearly that we are talking about a sub sequence and not a substring. Understanding the Longest Palindromic Subsequence problem better It is really easy to say if...
Read More

Binary Tree Linking Neighbors

Introduction Problem Statement : Given a regular binary tree with left, right and peer node pointers. The left and the right pointers are already populated. We need to make the peer pointer point to the next right neighbor on the same level. Understanding the problem Here is a diagram to explain what is needed. The red peer pointer points to the immediate right neighbor if the neighbor exists. Approach 1 The first solution which comes in mind is to do a custom, level order traversal as done in the post. And, in each iteration of the inner loop, just link each of the node to the next in ...
Read More

Matrix Transformation to make rows and columns zero

Introduction Another interview question about matrices. Matrices have been of great interest in programming and Linear Algebra and a matter of study for most of the programmers. The problem statement follows: "Given a matrix A of size M * N where M is the number of rows and N is the number of columns and each of its cell A[i][j] is populated with either zero or one. Transform the matrix such that if there exists a cell A[i][j] = 0 then the row i and column j is zero in the resultant matrix". You must not use extra space and try to solve it in lowest possible running time. Understanding...
Read More

Left Hand Projection of a Tree

Introduction Another interesting interview question, this question has many forms. One of the form is print the left hand projection of a tree or right hand projection of a tree or so on. Also, this problem can be specialized for a binary tree, ternary tree or n-ary tree, it really doesn't matter, the concept is the same. We will just try and print the left hand projection and I will add a note on how to print the right had project and you can try that yourself. Understanding the problem - Left Hand Projection of a Tree Let us consider the below tree for our problem statement. What is...
Read More