The Goal Parser Interpretation interview question is a basic string parsing task. You are given a string command that consists of the string "G", "()", and "(al)" in some order. The parser needs to interpret:
"G" as the string "G"."()" as the string "o"."(al)" as the string "al".
Your goal is to return the fully interpreted string.Google uses this String coding problem as a very quick initial screening question. It tests your basic programming syntax, specifically string replacement or simple loop iteration. It’s an "Easy" problem designed to ensure a candidate can write a functioning block of code quickly and without syntax errors.
This problem can be solved using String Replacement or a Linear Scan.
"()" with "o", and "(al)" with "al".command[i] == 'G', append 'G'.command[i] == '(':
command[i+1] == ')', append 'o' and skip to .command[i+1] == 'a', append 'al' and skip to .Command: "G()(al)"
i=0: 'G'. Append 'G'.i=1: '('. Check next char. i+1 is ')'. Append 'o'. Advance i by 2.i=3: '('. Check next char. i+1 is 'a'. Append 'al'. Advance i by 4.
Result: "Goal".command[i+1] during a manual linear scan, forgetting to ensure that i+1 is within the bounds of the string (though valid inputs here prevent it, it's a bad practice).result += char in a loop creates many temporary objects. Using a StringBuilder or the built-in .replace() is better.While .replace("()", "o").replace("(al)", "al") is a one-liner and perfectly valid, be prepared to write the loop-based parser if the interviewer asks you to optimize it for a single pass without allocating intermediate strings.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Asterisks | Easy | Solve | |
| Flip Game | Easy | Solve | |
| Generate a String With Characters That Have Odd Counts | Easy | Solve | |
| License Key Formatting | Easy | Solve | |
| Occurrences After Bigram | Easy | Solve |