Data Structures & AlgorithmsVery hardGoogle · Flipkart interview
List A has a nodes before it joins a shared tail of c nodes; list B has b nodes before the same tail, with a ≠ b. Pointer p walks A and, at its end, jumps to B's head; q walks B and then jumps to A's head; both move one node per step. Where do p and q first point to the same node?
- A. At the first shared node, after each has passed max(a, b) + c nodes
- B. At the first shared node, after each has passed a + b + c nodes
- C. At the last node of the shared tail
- D. Nowhere: with a ≠ b they stay out of step forever
Correct answer: B. At the first shared node, after each has passed a + b + c nodes
p passes a + c nodes of A and then b nodes of B; q passes b + c and then a. Both totals are a + b + c, so they arrive at the junction together, and since a ≠ b they cannot coincide earlier. If the lists do not intersect, both reach null together instead.
More Coding Interview Problems questions
- Problem: Move the coins of a binary tree so every node has exactly one coin; a move passes one coin along one edge. Count the minimum…
- Problem: Find the maximum |a − b| where a is an ancestor of b in a binary tree. python class T: def init(s, v): s.v, s.l, s.r = v, None…
- Problem: Return the (1-based) level of a binary tree with the largest sum of values (smallest level on ties). python class T: def init(s…
- Problem: Every minute, fresh oranges (1) next to rotten ones (2) rot (4 directions). How many minutes until no fresh orange remains (−1 if…
- Problem: Find the path from node 0 to node 3 with the maximum product of edge success probabilities (Dijkstra on −log p, or a max-heap on…
- Problem: Each bus route is a loop of stops. Find the fewest buses needed to travel from stop S to stop T (BFS over routes, not stops)…