Data Structures & AlgorithmsHardGATE CS · TCS NQT

A grid has rows ..#. / .##. / .... / #.#. where '.' is free and '#' is a wall. Moving up, down, left or right one cell at a time, what is the shortest distance from the top-left cell to the bottom-right cell (row 3, column 3, 0-based 0 to 3)?

  1. A. 7
  2. B. 11
  3. C. 6
  4. D. 16

Correct answer: C. 6

BFS from (0,0) explores cells in order of distance; one shortest route is (0,0)→(1,0)→(2,0)→(2,1)→(2,2)→(2,3)→(3,3), 6 moves. Counting cells instead of moves gives 7, the number of reachable free cells is 11, and 16 is the whole grid.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic