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?
- A. Yes, because each cell is enqueued at most once when visited flags are set
- B. No, because BFS can enqueue each cell many times
- C. No, because arrays cannot be used for queues
- 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
- To find the maximum over 10^5 queries of ranges in a static array, which approach is fast enough?
- A program multiplies two n x n matrices with n = 500 in int, and the product entries reach about 10^12. What is the error?
- To answer 'how many times does x appear in a sorted array' in O(log n), which pair of searches is correct?
- A bit array for flags of 10^9 items is held as one byte per item. What is the memory use, and is it fast enough in typical limits?
- struct P { char a; int b; char c; }; printf("%zu", sizeof(struct P)); What is printed with 4-byte int and natural alignment?
- struct P { char a; int b; }; printf("%zu", offsetof(struct P, b)); What is printed with 4-byte int and natural alignment (stddef.h…