Facebook Interview Question for iOS Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

-(UIView*)findMirrorView:(UIView*)view inParentView:(UIView*)A2
{
    NSMutableArray *steps = [NSMutableArray array];
    UIView *currentLevelV = view;
    
    while (currentLevelV.superview) {
        NSUInteger index = [currentLevelV.superview.subviews indexOfObject:currentLevelV];
        [steps addObject:@(index)];
        currentLevelV = currentLevelV.superview;
    }
    
    for (NSInteger i=[steps count]-1; i>-1; i--) {
        currentLevelV = A2.subviews[[steps[i] integerValue]];
    }
    
    return currentLevelV;
}

- ekbiker May 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This. Not glamorous, and no optimisations that I can see. The point is you're building a path from a node up to the root of a tree, and then you can use those indexes to traverse back down again. This is the one and only solution that I can see.

- fungled January 15, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hey,

Can you share what were the interview questions that were asked in your phone interview? Also this question of UIView seems to be a little confusing, can you please take out some time and explain it.

Thanks for your valuable time.

- Peter April 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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 May 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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?

- peeterparker May 05, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

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;
}

- peeterparker May 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Completely wrong.
You are considering just direct subviews of the view..what if you have:

view -> view -> view -> VIEW_TO_FIND

you have to edit your code to go in depth recursively.

- matteogobbi.jobs July 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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?

- matteogobbi.jobs July 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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
    }    
  }  
}

- carlos.carvalho.oak August 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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.

- Ahmed January 30, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More