Question d’entretien chez Meta

How to detect a common superview.

Réponses aux questions d'entretien

Utilisateur anonyme

13 nov. 2016

- (nullable UIView *)commonSuperview:(nullable UIView *)otherView { if(!otherView) return nil; NSMutableArray* superViewsArray = [NSMutableArray array]; UIView* superVeiw = self; while (superVeiw) { [superViewsArray addObject:superVeiw]; superVeiw = superVeiw.superview; } superVeiw = otherView; while (superVeiw) { if([superViewsArray containsObject:superVeiw]){ break; } superVeiw = superVeiw.superview; } return superVeiw; }

8

Utilisateur anonyme

16 nov. 2016

extension UIView { func sameSuperview(other: UIView) -> UIView? { guard self.superview != nil || other.superview != nil else { return nil } var superViews: [UIView] = [] //Should be a set var selfSuperView = self.superview while selfSuperView != nil { superViews.append(selfSuperView!) selfSuperView = selfSuperView!.superview } selfSuperView = other.superview while selfSuperView != nil { if superViews.contains(selfSuperView!) { return selfSuperView } selfSuperView = selfSuperView!.superview } return nil } }

1

Utilisateur anonyme

12 janv. 2017

- (UIView *)commonSuperView:(UIView *)oneView anotherView:(UIView *)anotherView { if (!oneView || !anotherView) { return nil; } NSMutableArray *viewArray = @[oneView].mutableCopy; while (oneView.superview) { oneView = oneView.superview; [viewArray addObject:oneView]; } if ([viewArray containsObject:anotherView]) { return anotherView; } while (anotherView.superview) { anotherView = anotherView.superview; if ([viewArray containsObject:anotherView]) { return anotherView; } } return nil; }

1

Utilisateur anonyme

10 juin 2017

// The total complexity of the algorithm is O(h1) + O(h2) func commonView(view1:UIView,view2:UIView?) -> UIView?{ if view2 != nil { var superView = [UIView:UIView]() var firstView = self.view var otherView = view2 while(firstView != nil){ superView[firstView!] = firstView?.superview ?? UIView() // h1 is the hieght of view1 so the complexitfy here is O(h1) firstView = firstView?.superview } while(otherView != nil){ //let say h1 is the height of view2 from its main parent. so the worst case complexity is O(h2) if (superView[otherView!] != nil){ return superView[otherView!] } otherView = otherView?.superview } } return nil }

1

Utilisateur anonyme

1 nov. 2016

This is like finding lca with parent link. Find depth of both views from root. Match level (Traverse upwards on longer chain). Technically you will be on same level as both root now just check parents,until they match go up the chain.

1

Utilisateur anonyme

7 nov. 2016

- (UIView *)commonSuperview:(UIView *)otherView { NSMutableSet *views = [NSMutableSet set]; UIView *view = self; do { if (view != nil) { if ([views member:view]) { return view; } [views addObject:view]; view = view.superview; if (otherView != nil) { if ([views member:otherView]) { return otherView; } [views addObject:otherView]; otherView = otherView.superview; } } } while (view || otherView); return nil; }

1

Utilisateur anonyme

26 sept. 2017

func commonSuperView(view1: UIView, view2: UIView) -> UIView? { var tempView1: UIView = view1 var tempView2: UIView = view2 while tempView1.superview != nil { while tempView2.superview != nil { if tempView1.superview === tempView2.superview { return tempView1.superview! } } tempView1 = tempView1.superview! tempView2 = view2 } return nil }

1

Utilisateur anonyme

6 mai 2018

func findCommonOne(view1: UIView, view2: UIView) -> UIView? { let tagConstant = 10000 var view1 = view1 var view2 = view2 view1.tag = tagConstant while let parentView = view1.superview { parentView.tag = tagConstant view1 = parentView } while let parentView = view2.superview { if parentView.tag == tagConstant { return parentView } view2 = parentView } return nil }

1

Utilisateur anonyme

22 juil. 2016

Simple unoptimized solution, followed by a solution using a hash map. Wasn't sure at first if it was possible to optimize.

1