Real-Number Calculator
Updated
Problem statement
Implement a calculator for real numbers. For this exercise, assume each request contains two operands, left and right, and one arithmetic operation operation.
"+": returnleft + right."-": returnleft - right."*": returnleft * right."/": returnleft / rightusing real-number division.
For this exercise, assume every request is valid: the operation is one of these four symbols, and a division request has a nonzero divisor. Evaluate just this one operation; there is no expression string to parse.
Return a double-precision result. An answer is accepted when its absolute error is at most 10^-6 * max(1, abs(expected)). Do not round the answer to a fixed number of decimal places.
Function
calculateReal(left: double, operation: String, right: double) → doubleExamples
Example 1
left = 2.5operation = "+"right = 1.75return = 4.25Adding 2.5 and 1.75 gives 4.25.
Example 2
left = -3.5operation = "-"right = 2return = -5.5Subtracting 2 from -3.5 gives -5.5.
Example 3
left = 1.2operation = "*"right = -2.5return = -3Multiplying 1.2 by -2.5 gives -3.
Example 4
left = 7.5operation = "/"right = 2return = 3.75Real-number division gives 7.5 / 2 = 3.75.
Constraints
-10^6 <= left <= 10^6.-10^6 <= right <= 10^6.- Both operands are finite real numbers.
operation.length == 1, andoperationis one of"+","-","*", or"/".- When
operation == "/",abs(right) >= 10^-6.
More NVIDIA Phone Screen questions
- Summarize Server Healthcheck LogsPHONE SCREEN · Seen Oct 2026
- Hash Partition OffsetsPHONE SCREEN · Seen Oct 2026
- Filter Paginated Medical RecordsPHONE SCREEN · Seen Oct 2026
- Least Frequently Used CachePHONE SCREEN · Seen Oct 2026
- Parallel String Counts with a Custom Hash TablePHONE SCREEN · Seen Oct 2026
- Simulate a Reference-Counted Smart PointerPHONE SCREEN · Seen Aug 2026
- Streaming Top-K Frequent ElementsPHONE SCREEN · Seen Aug 2026
- Merge Multiple Sorted StreamsPHONE SCREEN · Seen Aug 2026
- Linked List Palindrome in Constant SpacePHONE SCREEN · Seen Aug 2026
- LRU Key-Value Cache OperationsPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Circular Primes Below a LimitAmazon · PHONE SCREEN
- Palindrome NumberKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- Streaming AverageXantium · PHONE SCREEN
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Newton Square Root to a Guaranteed ToleranceSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Biased Coin: Probability of Exactly h HeadsAkuna Capital · PHONE SCREENAkuna Capital interview questions