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)?
- A. 7
- B. 11
- C. 6
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Two sorted linked lists A = [1, 4, 6, 9] and B = [2, 3, 7, 10, 12] are merged with the standard two-pointer merge, which compares the…
- How many distinct non-empty palindromic subsequences does the string abcba have (distinct as strings, not as index sets)?
- What is the sum of gcd(i, 12) over i = 1 to 12?
- Five customers arrive together at a bank with 2 tellers. Service times in arrival order are 3, 5, 2, 4 and 6 minutes. Each customer goes…
- A stack starts empty. Operations in order: push 1, push 2, pop, push 3, push 4, pop, pop. Write each popped value in order and read the…
- Buildings of heights 3, 7, 8, 3, 6, 1 stand in a row. A building is visible from the left if no building to its left is at least as tall…