FastPrepFirst Register Overflow Tick

First Register Overflow Tick

NVIDIA logoNVIDIA● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

An unsigned bits-bit register initially contains zero. At each unit tick, add the same positive integer increment. The first addition is at tick 1.

Overflow occurs when the mathematical sum would exceed 2^bits - 1. Return the first tick at which overflow occurs. Do not wrap the stored value before deciding whether that tick overflows.

Function

firstOverflowTick(bits: int, increment: int) → long

Examples

Example 1

bits = 8increment = 10return = 26

Tick 25 would store 250, while tick 26 would store 260 above the maximum 255.

Example 2

bits = 8increment = 255return = 2

Tick 1 reaches the valid maximum 255; tick 2 is the first overflow.

Example 3

bits = 4increment = 20return = 1

The maximum is 15, so the first addition already overflows.

Constraints

  • 1 <= bits <= 31.
  • 1 <= increment <= 10^9.
  • The result is a positive signed 64-bit integer.

More NVIDIA problems

See NVIDIA hiring insights
public long firstOverflowTick(int bits, int increment) {
    // Write your code here.
}
bits8
increment10
expected26
Checking account…