Checked Recursive Sum
Updated
Problem statement
Given a nonnegative integer n, compute the sum of every integer from 1 through n. The sum for n == 0 is 0.
For this exercise, the result must fit in the logical signed 32-bit range from 0 through 2147483647. Return -1 when the exact sum exceeds this range. Do not wrap, clamp or take a modulus. Make sure no intermediate integer calculation overflows, without relying on a wider integer type.
Implement a recursive summation approach with a safe stack depth, then express the tail-recursive accumulator state as a portable constant-stack loop. A recursive call written last in a function may still use another stack frame. The loop must perform the same updates without relying on the compiler to optimize tail calls.
This interface uses -1 to signal overflow.
Function
checkedSumUpTo(n: int) → intExamples
Example 1
n = 5return = 15The sum is 1+2+3+4+5=15.
Example 2
n = 65535return = 2147450880The exact sum 2147450880 is below 2147483647, so it can be returned as a signed 32-bit integer.
Example 3
n = 65536return = -1The exact sum 2147516416 exceeds 2147483647, so return the overflow sentinel -1.
Constraints
0 <= n <= 2147483647.- All inputs are valid. The return type is a logical signed
32-bit integer. - If the exact sum fits, return it exactly. Otherwise return
-1. - Use checked arithmetic or division comparisons within this logical range; Python arbitrary-size integers and Go machine-sized integers must follow the same explicit result limit.
More Headlands Technologies Phone Screen questions
- Integer Compression OffsetPHONE SCREEN · Seen Oct 2026
- Market Order BookPHONE SCREEN · Seen Jun 2026
- Gradient Descent on a Sinusoidal ObjectivePHONE SCREEN · Seen Apr 2026
- Pearson Correlation CoefficientPHONE SCREEN · Seen Apr 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Apr 2026
- Queue Mean from Corrective SnapshotsPHONE SCREEN · Seen Mar 2026
- Queue Mean QueriesPHONE SCREEN · Seen Mar 2026
- CPU Register Bitwise OperationsPHONE SCREEN · Seen Nov 2025
- Merge K Sorted SequencesPHONE SCREEN · Seen Oct 2024
- Top Trade Events by NotionalPHONE SCREEN · Seen May 2024
Similar questions at other companies
- Odd Numbers Up and BackNutanix · PHONE SCREENNutanix interview questions
- FactorialZillow · PHONE SCREEN · ONSITE INTERVIEWZillow interview questions
- Factorial Without MultiplicationWayve · PHONE SCREEN
- Path Between Two Nodes of a Fibonacci TreeDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Power with Integer ExponentsNVIDIA · PHONE SCREENNVIDIA interview questions
- Raise a Number to an Integer PowerAdobe · PHONE SCREENAdobe interview questions
- Pow(x, n)Bloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Preorder of a Special Binary TreeNielsen · OA
- Multiply Without the Multiplication OperatorMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- N-th Fibonacci NumberBank of America · ONSITE INTERVIEWBank of America interview questions