ProgrammingHard

A queue for BFS on a grid of 10^6 cells uses a fixed array of size 10^6 indexed by head and tail. Is this enough?

  1. A. Yes, because each cell is enqueued at most once when visited flags are set
  2. B. No, because BFS can enqueue each cell many times
  3. C. No, because arrays cannot be used for queues
  4. D. Yes, but only if the queue is sorted

Correct answer: A. Yes, because each cell is enqueued at most once when visited flags are set

With a visited flag set when a cell is enqueued, each of the 10^6 cells enters the queue at most once, so the array suffices.

More C Programming questions

Exams that ask this topic