Introduction
This is a supplement to the previous post where we were interested in just one path of a robot from top left corner to the bottom right corner. In this post we will print all paths of robot moving through a given grid containing few blocked cells.
The algorithm for this solution remains the same, we just need few tricks to get that right. Here, I will explain the idea behind printing all paths of robot through the grid.
Approach
Here, let us discuss what different we need to do to print all the paths. As we can recall from the previous post, the moment we reached the destina...

Read More
# amazon interview questions

## Finding loop in Linked List

Problem Statement
You are given a singly linked list which may or may not contain a loop. The task at hand is to find if the loop exists. Here is a diagrammatic representation of a loop in a singly linked list.
[caption id="attachment_3617" align="aligncenter" width="688"] Finding loop in LinkedList[/caption]
It is pretty clear that there is a loop which starts at the node 3 and there are 10 nodes which make the loop. In this case we need to print a YES. If it had been a singly linked list without the node 12 connecting to node 3, we would have printed a NO.
Approach to Finding loop i...

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
## 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
## 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