Uber Phone Screen | Basic Calculator (Variation)

Anonymous User
1046
Oct 30, 2024

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)"

# 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”

Came super close to a working solution with a few bugs.
Time: O(n)
Space: O(n)

def solution(s):
    signStack = []
    stack = []
    num = 0
    index = 0
    while index < len(s):
        char = s[index]
        if char.isdigit():
            num = num * 10 + int(char)
        elif char in {"a", "s"}:
            if s[index:index + 4] == "add(":
                signStack.append("+")
				# <bug> This should have been a 3!!!!!
                index = index + 4
            elif s[index:index + 4] == "sub(":
                signStack.append("-")
				# <bug> This should have been a 3!!!!!
                index = index + 4
            else:
                return exception(f"syntax error at position {index} for in string")
        elif char == ",":
            stack.append(num)
            num = 0
        elif char == ")":
			# bug when it's sub(a, b) and num would be 0
            first, second = stack.pop(), num
            currSign = signStack.pop()
            if currSign == "+":
                stack.append(first + second)
            elif currSign == "-":
                stack.append(second - first)
            num = 0
        index += 1

    return sum(stack)

Actual Solution, I think!


def solution(s):
    signStack = []
    stack = []
    num = 0
    index = 0
    while index < len(s):
        char = s[index]
        if char.isdigit():
            num = num * 10 + int(char)
        elif char in {"a", "s"}:
            # add, sub
            if s[index:index + 4] == "add(":
                signStack.append("+")
                index += 3
            elif s[index:index + 4] == "sub(":
                signStack.append("-")
                index += 3
            else:
                return exception(f"syntax error at position {index} for in string {s}")
        elif char == ",":
            stack.append(num)
            num = 0
        elif char == ")":
            if num:
                stack.append(num)
                num = 0
            first, second = stack.pop(), stack.pop()
            currSign = signStack.pop()
            if currSign == "+":
                stack.append(first + second)
            elif currSign == "-":
                stack.append(second - first)
        elif char != " ":
            return exception(f"syntax error at position {index} for in string {s}")
        index += 1
    return sum(stack)
Comments (8)