Daily iOS
UIKit

Finding the common UIView parent

You're writing a function that finds the nearest common parent of two UIViews. Which data structure would you keep one view's superview chain in?

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

Walk up from the first view through superview and put each view in a Set<ObjectIdentifier>, then walk up from the second view starting with itself and return the first view found in that Set; that's the nearest common parent.

Share a question