For example:
- “-1-2-3” => -6
- “1+2-13” => -10
I asked a clarifying question, and the interviewer mentioned that the expression only involves addition and subtraction, without parentheses, and the expression is always valid.
Then, a follow-up question was about what to do if the expression includes parentheses. Initially, I asked about the presence of parentheses, and since there were none, I proceeded with algebra. Later, I switched to using a stack.
I rewrote the solution. During this process, I discussed the advantages and disadvantages of different approaches.
The interviewer clearly had a standard answer in mind, so it’s essential for everyone to practice more problems, understand various solutions, and avoid memorizing answers.
Good luck to everyone!