Daily iOS
UIKit

Flood fill on a large grid

You're implementing flood fill on a large pixel grid drawn with a collection view (say, 500×500). If you don't use recursive DFS, how would you write the traversal?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Recursive DFS on 500×500 piles up a call stack as deep as the pixel count and overflows, so switch to an iterative BFS: put the start coordinate in a queue, pop it, push the four same-colored neighbors, and mark visited by changing the color right away.

Share a question