DevPrep

All topics › What does it print? Interview puzzles

lazy.filter.map.prefix: how many times is map called?

Middle PRO

Task

var calls = 0
let result = (1...9).lazy
    .filter { $0 % 2 == 1 }
    .map { value -> Int in
        calls += 1
        return value * value
    }
    .prefix(3)

print(Array(result), calls)

What gets printed? And without .lazy?

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