Interview Questions

This category contains few tricky questions asked in interviews..

Merge Point of two Linked Lists

Problem Statement This is another interview question which can be linked to the Cycle Detection in Linked List question. In fact this is an extension of the Cycle Detection problem and requires an extra tweaking. Here is a diagram explaining the the problem of finding the merge point of two linked lists In the above diagram, the head of one linked list is node 1 and the head of another linked list is node 5. The merge point is node 4. We would like to write a program to find node 4 given both the heads. Approach There are multiple ways of solving it, here I list three of them: Br...
Read More

Pattern Search in Grid

Problem Statement Given a huge grid G with M rows and N columns and a pattern P with m rows and n columns. The task at hand is to perform a pattern search in grid, and print YES if the pattern grid P exists in the grid G otherwise print NO [caption id="attachment_3611" align="aligncenter" width="960"] Pattern Search in Grid[/caption] Its a simple string search but requires special attention to the boundary cases. Here is a list of them We only need to test the grid G for M-m rows, because anything below row R-r  cannot accommodate the pattern of size r. We only need to test the gr...
Read More

Ring wise Matrix Rotation

Introduction Matrices have always been of interest in the field of encryption, specifically matrix rotations which leads to a symmetric as well as asymmetric cryptography. To learn more about regular matrix rotation please check my recent posts on the subject. This however is a fancy Ring wise Matrix Rotation and is no where related to the regular rotation of matrices. Problem Statement You are given a M*N grid with the restriction that the minimum of M and N is an even number. Rotate each of the rings by a distance K. We can choose the direction of rotation to be either clockwise or anti-c...
Read More

Cloning Linked List having Next and Random Pointer

Introduction This question was asked by a colleague, although it seems to be simple at first but with given restrictions like running time and space limitations, it becomes tricky. You need to be good with Linked Lists to crack the right solution. Here we try to define the problem in more detail. Given a Linked List where each node contains two pointers next and random. The next pointer points to the immediate next node and the random pointer points to any random node in the linked list. Clone the linked list and return the head of the clone. There are definitely certain conditions impos...
Read More

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

Find if a Tree is Subtree of another Tree

Introduction This is a simple yet non trivial problem. Given two trees we need to evaluate if one tree is the sub tree of the other. If the condition is satisfied, it returns true, else it returns false. The definition of sub tree must not be ambiguous. We say that two trees are equal if they are structurally equal and their corresponding nodes have equal keys. Equality of trees Remember that subtree and contains relation are not the same. It may be possible that a tree T1 contains another tree T2 and still T2 is not a sub tree of T1. This also means that all subtree satisfies the con...
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