Facebook Interview Question
iOS DevelopersCountry: United States
Interview Type: In-Person
Yeah, I had a hard time trying to understand what's being asked.
In particular, what "mirroring in A2" means in this context. Perhaps what's being asked is that if A1 is contained in some UIView hierarchy (A1 is directly contained in another view, which is contained in another view... which is contained in a distinguished view UIView A, then try to find an analogous A' as container of containers for A2. But I don't know :-)
Diego,
So let me rephrase this question. Suppose A1 has many subviews in its tree hierarchy and among them we have one view, say MyViewToFind which say exists at ith location on A1's tree.
Taking the ith view (MyViewToFind) from A1, do I have to check to see (or find out the same view) in A2's view hierarchy?
After careful thoughts and trying to understand the question, I believe the question can be solved in O(n) algorithm complexity and O(n) space
- (void)commonView:(UIView *)a1 with:(UIView *)a2 {
NSMutableSet *s = [[NSMutableSet alloc] initWithCapacity:[[a1 subviews] count]];
for (UIView *a1SubViews in [a1 subviews]) {
[s addObject:[a1SubViews class]];
}
for (UIView *a2SubView in [a2 subviews]) {
if ([s containsObject:[a2SubView class]]) {
NSLog(@"#Boom, I found the common view %@", a2SubView);
return;
}
}
NSLog(@"NO common subview");
return;
}
Guys, I'm not able to understand the problem.
An UIView can't be the subview in 2 different hierarchy. It would says that it has 2 or more superview. As you know instead, an UIView has just 1 superview..1 parent..and it can stay just in 1 hierarchy.
So of what we are talking about? About the CLASS? Or what? Someone have understood the question?
import Foundation
public class UIView {
private(set) var tag: Int
private(set) var superview: UIView?
private(set) var subviews = [UIView]()
init(tag: Int) {
self.tag = tag
}
public func addSubview(_ view: UIView) {
subviews.append(view)
view.superview = self
}
}
let A1 = UIView(tag: 1)
let A2 = UIView(tag: 2)
let A3 = UIView(tag: 3)
let A4 = UIView(tag: 4)
let A5 = UIView(tag: 5)
A1.addSubview(A2)
A2.addSubview(A3)
A3.addSubview(A4)
A3.addSubview(A5)
let b1 = UIView(tag: 6)
let b2 = UIView(tag: 7)
let b3 = UIView(tag: 8)
let b4 = UIView(tag: 9)
let b5 = UIView(tag: 10)
b1.addSubview(b2)
b2.addSubview(b3)
b3.addSubview(b4)
b3.addSubview(b5)
var hashtable: Dictionary<Int, [Int]>? = nil
func findView(_ view: UIView, in target: UIView) -> UIView {
buildCacheFromView(view)
let path = hashtable![view.tag]!
var v: UIView? = view
for idx in path {
v = v!.subviews[idx]
}
return v!
}
func buildCacheFromView(_ view: UIView) {
let path = [Int]()
buildCacheFromView(view, path)
}
func buildCacheFromView(_ view: UIView, _ path: [Int]) {
if hashtable == nil {
hashtable = Dictionary<Int, [Int]>()
for v in view.subviews {
let idx = view.subviews.index(of: v)!
path.append(idx)
buildCacheFromView(v, path)
hashtable![view.tag] = path
}
}
}
I saw this question in other candidates feedback.
The question is: Say you have a root view A, and it has been cloned in another view controller it clone is B. Where cloned means, B has the exact same hierarchy as A, but not the same views in memory.
Now your function will take B and c (c is a subview located somewhere in A hierarchy) as its arguments. Return d where d is the view in B hierarchy in the exact same location as c in A.
- ekbiker May 05, 2014