Daily iOS
Architecture

Extensible search API

Here's a file search function. Now it has to find only "UTF-8 txt or H.264 mp4" and "files modified within the last week," and the conditions will keep growing. How do you extend this API without adding parameter after parameter?

func findFiles(in dir: URL, suffix: String) -> [URL]
How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Take the condition as a (FileInfo) -> Bool predicate (a closure or a protocol) instead of parameters, and let the caller compose it. Expensive checks (encoding, codec) are computed lazily so they come after cheap ones like name and modification date.

Share a question