Daily iOS
CS fundamentals and algorithms

View hierarchy tree search

You're given a specific UIView instance in an original view hierarchy, plus a copy of that hierarchy with the same structure. How would you design a function that finds the corresponding view in the copy?

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

Record the path from the root to the target (the subviews index at each level) and follow the same indexes down the copy, which is O(depth); without the path, walk both trees in lockstep and return the copy's node at the moment the original's node is the target. Differing structure and view reuse are the edge cases.

Share a question