Google | Phone Interview | Split by " "
Anonymous User
2082
Dec 24, 2021
Dec 24, 2021

The goal is to design a function similar to Python's split function but in this case we're splitting a given input string by ' '. However, the catch is, if there is a ' ' between quotes within the string then you must preserve it.

Example:
input_string = "foo hello 'hello world' blah"
output: ["foo", "hello", "hello world", "blah"]

There are a couple edge cases the interviewer wanted me to consider:

  • leading spaces
  • trailing spaces
  • extra spaces between words
  • how to deal with a string that has missing starting/ending quotes (he suggested I raise an exception)

I came up with a two pointer approach for this problem. Do you guys have any other solutions/suggestions?

Comments (10)