Split a String by a Delimiter
Updated
Problem statement
Implement your own string split. Given a string s and a non-empty string delimiter, cut s at every place where delimiter appears and return the pieces in order.
- The delimiter is taken literally, character for character. Characters such as
|and.have no special meaning. - Matches are found from the left and never overlap: after a match, the search continues right after it.
- Keep every piece, including empty ones: two delimiters next to each other, or a delimiter at the start or end of
s, produce empty pieces. - If the delimiter never appears, the answer is
[s]. An emptysgives[""].
Do not use your language's built-in split function.
Follow-up: how would you extend your function so the delimiter can also be a regular expression?
Function
splitString(s: String, delimiter: String) → String[]Examples
Example 1
s = "red,green,,blue"delimiter = ","return = ["red","green","","blue"]There are three commas. Cutting at each one gives red, green, an empty piece (between the two commas next to each other), and blue.
Example 2
s = "a--b--c"delimiter = "--"return = ["a","b","c"]The delimiter is the two-character substring --. It appears twice, giving three pieces.
Example 3
s = "aaa"delimiter = "aa"return = ["","a"]Matches are found from the left and do not overlap: aa at index 0 is a delimiter, and the remaining a is too short for another match. So the pieces are the empty string before the match and a after it.
Constraints
0 <= s.length <= 10^41 <= delimiter.length <= 100sanddelimiterconsist of lowercase English letters, digits, spaces, and the characters, ; : | . -
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Rich Text Parser Part 1 (Validate Tokens)Figma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
- Message Latencies from CSV LogsAmazon · ONSITE INTERVIEW
- URL / Path Segment Compression Part 1Stripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Most Cost-Effective MachineCapital One · ONSITE INTERVIEWCapital One interview questions
- Word Appears Exactly N TimesNutanix · ONSITE INTERVIEWNutanix interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW
- Serialize and Deserialize a String Key-Value StoreByteDance · ONSITE INTERVIEWByteDance interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions