DevPrep

All topics › Real interview tasks

Parse a string into brand, model and generation

Middle PRO

Task

You have a string like "BMW 5 series e39" and reference lists: the brand's known models and generations. Return (brand, model, generation). The catch: a model may be several words ("5 series") and may be a prefix of another ("5" and "5 series"). Any part may be missing.

let models = ["5 series", "3", "5", "7", "9"]
let generations = ["e39", "g30"]

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