Operator-First Array Calculator
Updated
Problem statement
You are given an operator op, which is "+" or "-", and a non-empty array operands. Together they form an expression written with the operator first and the numbers after it. Return the value of that expression.
- For
"+", add up all the operands. - For
"-", start with the first operand. Then subtract each later operand, one at a time, from left to right.
Function
calculate(op: String, operands: int[]) → longExamples
Example 1
op = "+"operands = [1,2,3]return = 6Add up all the operands: 1 + 2 + 3 gives six, which is the answer.
Example 2
op = "-"operands = [10,5,3]return = 2Start with the first operand, then subtract the others from left to right: (10 - 5) - 3 = 2, which is the answer.
Example 3
op = "-"operands = [-4,-6,3]return = -1Subtract from left to right. Subtracting a negative number is the same as adding its positive value: (-4 - (-6)) - 3 = -1, which is the answer.
Constraints
opis exactly"+"or"-".1 <= operands.length <= 200000.-1000000000 <= operands[i] <= 1000000000.- The final answer fits a signed 64-bit integer.
More Anduril interview questions
- Add Word and Suffix SearchONSITE INTERVIEW · Seen Aug 2026
- Binary Tree Preorder TraversalONSITE INTERVIEW · Seen Aug 2026
- Daily TemperaturesONSITE INTERVIEW · Seen Jun 2024
- Highlighted Context WindowsONSITE INTERVIEW · Seen Jun 2024
- Pacific Atlantic Water FlowPHONE SCREEN · Seen Aug 2026
- Choose the Front Football TeamPHONE SCREEN · Seen May 2026
- Maximum Visible DronesPHONE SCREEN · Seen Jun 2024
- Build a Sorted Circular Singly Linked ListPHONE SCREEN · Seen Feb 2024
- Sparse Vector Dot ProductPHONE SCREEN · Seen Apr 2021
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEWGoogle interview questions
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Copy File Bytes with Partial WritesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Policy Violation Set ChecksPinterest · ONSITE INTERVIEWPinterest interview questions
- Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
- Extract Error Log ContextFlexTrade · ONSITE INTERVIEWFlexTrade interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions