Apple Interview Question for Software Developers


Country: United States




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

This question was asked to one of my friends, so I am not sure about the exact question details. I have the following questions:

1. How is a function passed to another function?
2. Can't we just use the sleep method?

I think this to be a system design question.

- lkjhgfdsa December 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Essentially the same question:
careercup.com/question?id=4509450021896192
Since interviewers gives milliseconds and might have a devices background, depending on allowed jitter, you might need to use RTC events, if that's easily available on your OS. You can craft this into the entioned algo.

- Chris December 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, it makes sense. Thank you as always, ChrisK! :)

- lkjhgfdsa December 11, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I guess that's why it's a decent question to ask.

Some fundamentals behind this would be knowing what a function inside of a function is? Apple refers to this as a closure expression, and can be short handed as a trailing closure.

Good examples of these are URLSessions and UIView.animate methods. You're essentially passing a closure (which is a function) and saying, "I'm not sure what the value is, but here's a recipe on how to use the value when you get it."

Sleeping would work but you'll most likely doing that on main which isn't the best idea. A good example of using both of these together would be to actually just do:

func execute(after time: Int, f: @escaping () -> Void) {
	DispatchQueue.main.async(deadline: .now() + .milliseconds(time), execute: f)
}

// Usage:
execute(after: 250) { [weak self] in
	self?.title = "Example"
}

The weak self is known as a capture list which protects from creating reference cycles and executing when the instance has been deinitialized.

- Kris July 10, 2018 | 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