DevPrep

All topics › What does it print? Interview puzzles

What does this GCD code print? (main.sync and the run loop)

Senior PRO

Task

func printMultithreading() {
    print("2")
    DispatchQueue.global().async {
        print("3")
        DispatchQueue.main.sync { print("4") }
        print("5")
    }
    print("6")
}

print("1")
printMultithreading()
print("7")
RunLoop.main.run(until: Date(timeIntervalSinceNow: 5))

What prints, in which order, and what changes without the last line?

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store