FastPrepChecked Recursive Sum
Problems/Checked Recursive Sum

Checked Recursive Sum

Headlands Technologies logoHeadlands Technologies● EasyFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The phone report asks for a recursive sum from 1 through n. It then discusses a wider long type, signaling failure when long cannot be used, and a tail-recursive accumulator to avoid stack overflow. We chose the follow-up with no wider type, a logical signed 32-bit result, and -1 on overflow. The exact width, interface, input limits, the balanced-recursion alternative, and a portable loop for the tail state are our choice. Tail-call optimization is not guaranteed across languages. Our version is about 80% the same as the reported question.

A separate February 2021 phone report describes a recursive accumulation function, C++ parameter passing and return values, error handling with exceptions, stack use in recursion, and turning it into a tail-recursive accumulator. The author also mentions template-generated code. That report gives no original function or number range. The 1-through-n rule, the checked signed 32-bit rule, and -1 here are our choices, tied to the more specific 2023 report. We do not claim them as details of the general 2021 interview. Template generation is context, not something you must do, and tail recursion does not guarantee that the compiler removes tail calls.

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) → int

Examples

Example 1

n = 5return = 15

The sum is 1+2+3+4+5=15.

Example 2

n = 65535return = 2147450880

The exact sum 2147450880 is below 2147483647, so it can be returned as a signed 32-bit integer.

Example 3

n = 65536return = -1

The 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
All Headlands Technologies interview questionsSee Headlands Technologies hiring insights
Saved
class Solution {
    public int checkedSumUpTo(int n) {
        // write your code here
    }
}
Checking account…