FastPrepSplit a Log Outside Quotes
Problem · String

Split a Log Outside Quotes

Learn this problem
MediumGoogle logoGoogleFULLTIMEONSITE INTERVIEW
See Google hiring insights

Problem statement

Given a printable-ASCII logging string log, split it on ASCII space characters that occur outside double-quoted regions. Spaces inside a balanced pair of double quotes belong to the current token.

  • Quote characters group content and are omitted from returned tokens.
  • There are no escape sequences, and all double quotes are balanced.
  • Leading, trailing, or consecutive spaces outside quotes do not create empty tokens.
  • A quoted empty string "" does create one empty token.

Return the tokens in encounter order.

Function

splitLog(log: String) → String[]

Examples

Example 1

log = "level info \"request completed\" code 200"return = ["level","info","request completed","code","200"]

The space inside "request completed" is retained, while the other spaces delimit tokens.

Example 2

log = "  a  \"b c\" \"\" d  "return = ["a","b c","","d"]

Repeated unquoted spaces are ignored, while the explicitly quoted empty field is preserved.

Constraints

  • 0 <= log.length <= 200000
  • log contains printable ASCII characters.
  • Double quotes occur in balanced pairs.
  • Escape sequences are outside the scope of this exercise.

More Google problems

drafts saved locally
public String[] splitLog(String log) {
    // Write your code here
}
log"level info \"request completed\" code 200"
expected["level", "info", "request completed", "code", "200"]
checking account