romil.bm
BAN USERgenerally the right idea. and I agree with @mithya, no queue. also as @tr030114 mentioned, getting to know the current system is important before beginning to optimize it. For example, what does wait time actually mean? does it simply mean, calling for an elevator and getting into it? Coz, remember this is a really tall building and someone wanting to go to the higher floors from the ground floor might be experiencing a lot to transit time while the elevator picks and drops people on the way up. Your solution although generally good is not clearly addressing this.
 romil.bm April 27, 2015This is just a variation of Breadth First traversal on a tree.
void setNextNode(Node root) {
Queue<Node> mainQueue = new ArrayDequeue<Node>();
if (root == null) return;
mainQueue.add(root);
int nodesInCycle = 1;
while (!mainQueue.isEmpty()) {
Queue<Node> holdingQueue = new ArrayDequeue<Node>();
for (int i = 0; i < nodesInCycle; i++) {
Node current = mainQueue.remove();
current.next = mainQueue.peek();
if (current.left != null)
holdingQueue.add(current.left);
if (current.right != null)
holdingQueue.add(current.right);
}
mainQueue.addAll(holdingQueue);
nodesInCycle = mainQueue.size();
}
}

romil.bm
April 17, 2015 ..and there is a general solution for (nk)th node. start the 2nd pointer k ahead of the 1st pointer. Move them both 1 step each time. when 2nd pointer has no more moves to make, 1st has reached the required node.
In this case, k=1. so p=1, q=2.
then p=2, q=3
...
p=9, q=10.
End.
Open Chat in New Window
Get the elements from each iterator in the input array one at a time, each time checking if you still can use that iterator(using hasNext). Store them in an ArrayList. Instantiate an iterator over this list of combined elements and use it to serve the next element every time.
 Output 
 romil.bm May 03, 201510
30
40
20
50
60