Daily iOS
UIKit

Finding the common superview

Given several UIViews, how would you design a function that returns their nearest common superview?

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

Remember the first view's superview chain, then walk up each remaining view's chain and narrow the candidate to the first view you meet (the LCA). Return nil if they never meet because they're in different windows.

Share a question