Find the first day a climber reaches a target height when climbing by day and sliding back at night

Quick Overview

Given a target height, a daily climb and a smaller nightly slide, find the first day on which a climber reaches the target during that day's ascent. It tests modeling a repeating process with arithmetic, handling the final day without its night slide, and coping with inputs too large for a step-by-step loop.

Find the first day a climber reaches a target height when climbing by day and sliding back at night

Company: Capital One

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A climber wants to reach a height of `target` units. Each day, the climber ascends `daily_gain` units. Each night, the climber slides down `night_loss` units while resting. The climbing always starts on day 1. Return the first day on which the climber's height reaches or exceeds `target` at any point during that day's ascent (before the night slide). The climber stops as soon as the target is reached. ### Function Signature ```python def first_day_reaching_target(target: int, daily_gain: int, night_loss: int) -> int: ``` ### Rules - The climber starts at height 0 at the beginning of day 1. - Days are numbered from 1. Each day is one ascent of `daily_gain` units, followed by a slide of `night_loss` units only if the target has not been reached yet. - The answer is the day whose ascent first brings the height to `target` or higher. The slide on that night never happens. ### Constraints - `1 <= target <= 10⁹` - `1 <= daily_gain <= 10⁴` - `0 <= night_loss < daily_gain` ### Examples ```text Input: target = 10, daily_gain = 4, night_loss = 1 Output: 3 ``` Day 1 climbs to 4 and slides to 3. Day 2 climbs to 7 and slides to 6. Day 3 climbs to 10 and reaches the target. ```text Input: target = 5, daily_gain = 5, night_loss = 2 Output: 1 ``` The first ascent brings the climber to exactly 5, so the target is reached on day 1 and the slide never happens. ```text Input: target = 1000000000, daily_gain = 10000, night_loss = 9999 Output: 999990001 ``` Each day and night together net 1 unit, so the climber starts day k at height k - 1 and peaks at k + 9999. That peak first reaches 1,000,000,000 when k = 999,990,001.

Overview: Given a target height, a daily climb and a smaller nightly slide, find the first day on which a climber reaches the target during that day's ascent. It tests modeling a repeating process with arithmetic, handling the final day without its night slide, and coping with inputs too large for a step-by-step loop.

Capital One logo
Capital One
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A climber wants to reach a height of target units. Each day, the climber ascends daily_gain units. Each night, the climber slides down night_loss units while resting. The climbing always starts on day 1.

Return the first day on which the climber's height reaches or exceeds target at any point during that day's ascent (before the night slide). The climber stops as soon as the target is reached.

Function Signature

def first_day_reaching_target(target: int, daily_gain: int, night_loss: int) -> int:

Rules

  • The climber starts at height 0 at the beginning of day 1.
  • Days are numbered from 1. Each day is one ascent of daily_gain units, followed by a slide of night_loss units only if the target has not been reached yet.
  • The answer is the day whose ascent first brings the height to target or higher. The slide on that night never happens.

Constraints

  • 1 <= target <= 10⁹
  • 1 <= daily_gain <= 10⁴
  • 0 <= night_loss < daily_gain

Examples

Input: target = 10, daily_gain = 4, night_loss = 1
Output: 3

Day 1 climbs to 4 and slides to 3. Day 2 climbs to 7 and slides to 6. Day 3 climbs to 10 and reaches the target.

Input: target = 5, daily_gain = 5, night_loss = 2
Output: 1

The first ascent brings the climber to exactly 5, so the target is reached on day 1 and the slide never happens.

Input: target = 1000000000, daily_gain = 10000, night_loss = 9999
Output: 999990001

Each day and night together net 1 unit, so the climber starts day k at height k - 1 and peaks at k + 9999. That peak first reaches 1,000,000,000 when k = 999,990,001.