Round 1
Questions: This is a variation on the Basic Calculator where we essentially are thinking about parsing the input in a pre-order manner [sign, operand1, operand2].
- Suggested a recursive solution, was pressed to determine if linear was possible.
- Came to an approach with two stacks (with a hint to think about how we decide to add or subtract two operands in the string).
Input/Output Examples:
# Parse strings that for a simple language as follows : # input -> "add(1,3)" output -> 4 # input -> "sub(1,3)" output -> -2 # Input -> “add(3, add(3, 2))” -> 8 # Input -> “ add(3, sub(3, add(3, 2))) ” -> 1 # Input -> “add( bad,2342)” -> return exception “syntax error at position 6 for "bad" in the string”
Candidate's Approach
- Came super close to a working solution with a few bugs.
- Time Complexity: O(n)
- Space Complexity: O(n)
The candidate implemented a solution using two stacks to handle the signs and operands. The approach involved parsing the string character by character, handling digits, operators, and parentheses accordingly.
Interviewer's Feedback
No feedback provided.