Data Structures & AlgorithmsHardGoogle · Flipkart interview
Shortest paths in a large grid where each move costs either 0 or 1. Which algorithm is the most efficient correct choice?
- A. Plain BFS, because all costs are small
- B. 0-1 BFS with a deque (0-cost moves to the front), O(V + E)
- C. Bellman–Ford, because some costs are zero
- D. DFS that follows 0-cost edges first
Correct answer: B. 0-1 BFS with a deque (0-cost moves to the front), O(V + E)
A deque keeps nodes in non-decreasing distance order, mimicking Dijkstra without a heap. Plain BFS counts edges, not costs; Bellman–Ford is O(V·E); greedy DFS can lock in a non-shortest route.
More Coding Interview Problems questions
- You must report the 100 largest values from a stream of 10⁹ integers using about 1 MB of memory. Which approach fits?
- Edge case: JavaScript type coercion. javascript console.log(typeof null, [] + {}, [] == ![], 0.1 + 0.2 === 0.3); What is printed?
- Edge case: += versus + on a list that has another name. python a = [1, 2, 3] b = a a += [4] a = a + [5] print(b, a) What is printed?
- Edge case: changing a dict while iterating over it. python d = {1: 'a', 2: 'b', 3: 'c'} try: for k in d: if k == 2: d[4] = 'd' print("ok"…
- Edge case: Python's round() and sorting strings with mixed case. python print(round(2.5), round(3.5), round(-0.5), sorted(['b', 'A', 'a'…
- Problem: Shops sit on a circular ring with profits p = [5, -3, 5, 4, -8, 6]. Choose a contiguous arc of at least one shop (the arc may…