Compute the distance a bird flies collecting sticks in alternating directions

Quick Overview

Simulate a bird that alternates right and left trips from its nest to pick up the nearest stick in an array, stopping once the collected length reaches 100 or the sticks run out. It tests careful simulation, pointers that only move outward, and handling a direction that has no sticks left.

Compute the distance a bird flies collecting sticks in alternating directions

Company: Capital One

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an integer array `forest` of non-negative integers and an integer `bird`. Each positive value `forest[i]` is a stick of that length lying at position `i`, and a value of `0` means the position is empty. The value `forest[bird]` is `0` and marks the position of the bird's nest. The bird builds its nest by making trips. On the first trip it flies to the right, on the second trip it flies to the left, and it keeps alternating directions. On each trip the bird starts at the nest, travels in the current direction until it reaches the nearest position holding a stick, picks that stick up (the position becomes `0`), and flies straight back to the nest. If the nearest stick found on a trip is at position `i`, that trip costs `2 * abs(i - bird)` distance. If there is no stick remaining in the current direction, the bird skips that trip (no distance is added) and switches to the other direction on its next trip. The bird stops as soon as the total length of the collected sticks is greater than or equal to `100`, or when no sticks remain in the forest. Return the total distance flown. ### Function Signature ```python def total_distance(forest: list[int], bird: int) -> int: ``` ### Rules - Right means toward higher indices and left means toward lower indices. - A skipped trip still uses up a turn, so the next trip goes the other way. - The stopping condition is checked after every collected stick. Once the collected length reaches `100`, the bird makes no further trips. - If the forest holds no sticks at all, the answer is `0`. ### Constraints - `1 <= len(forest) <= 10^5` - `0 <= forest[i] <= 100` - `0 <= bird < len(forest)` - `forest[bird] == 0` ### Examples ```text Input: forest = [30,0,40,0,0,50,0], bird = 1 Output: 12 ``` Trip 1 goes right to index 2 (length 40) for distance 2, trip 2 goes left to index 0 (length 30) for distance 2, and trip 3 goes right to index 5 (length 50) for distance 8. The collected length is now 120, which is at least 100, so the total is 2 + 2 + 8 = 12. ```text Input: forest = [0,10,10,10], bird = 0 Output: 12 ``` There are no sticks to the left of the nest, so every left trip is skipped. The bird collects indices 1, 2 and 3 for distances 2, 4 and 6, but only gathers 30 units before the forest is empty, so the total is 12. ```text Input: forest = [30,50,0,60,0], bird = 4 Output: 8 ``` The nest is at the last index, so the first trip (right) is skipped. The left trips collect index 3 (length 60, distance 2) and then index 1 (length 50, distance 6), which brings the total length to 110. The bird stops with the stick at index 0 still on the ground, after flying 8.

Overview: Simulate a bird that alternates right and left trips from its nest to pick up the nearest stick in an array, stopping once the collected length reaches 100 or the sticks run out. It tests careful simulation, pointers that only move outward, and handling a direction that has no sticks left.

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

You are given an integer array forest of non-negative integers and an integer bird. Each positive value forest[i] is a stick of that length lying at position i, and a value of 0 means the position is empty. The value forest[bird] is 0 and marks the position of the bird's nest.

The bird builds its nest by making trips. On the first trip it flies to the right, on the second trip it flies to the left, and it keeps alternating directions. On each trip the bird starts at the nest, travels in the current direction until it reaches the nearest position holding a stick, picks that stick up (the position becomes 0), and flies straight back to the nest. If the nearest stick found on a trip is at position i, that trip costs 2 * abs(i - bird) distance.

If there is no stick remaining in the current direction, the bird skips that trip (no distance is added) and switches to the other direction on its next trip.

The bird stops as soon as the total length of the collected sticks is greater than or equal to 100, or when no sticks remain in the forest. Return the total distance flown.

Function Signature

def total_distance(forest: list[int], bird: int) -> int:

Rules

  • Right means toward higher indices and left means toward lower indices.
  • A skipped trip still uses up a turn, so the next trip goes the other way.
  • The stopping condition is checked after every collected stick. Once the collected length reaches 100 , the bird makes no further trips.
  • If the forest holds no sticks at all, the answer is 0 .

Constraints

  • 1 <= len(forest) <= 10^5
  • 0 <= forest[i] <= 100
  • 0 <= bird < len(forest)
  • forest[bird] == 0

Examples

Input: forest = [30,0,40,0,0,50,0], bird = 1
Output: 12

Trip 1 goes right to index 2 (length 40) for distance 2, trip 2 goes left to index 0 (length 30) for distance 2, and trip 3 goes right to index 5 (length 50) for distance 8. The collected length is now 120, which is at least 100, so the total is 2 + 2 + 8 = 12.

Input: forest = [0,10,10,10], bird = 0
Output: 12

There are no sticks to the left of the nest, so every left trip is skipped. The bird collects indices 1, 2 and 3 for distances 2, 4 and 6, but only gathers 30 units before the forest is empty, so the total is 12.

Input: forest = [30,50,0,60,0], bird = 4
Output: 8

The nest is at the last index, so the first trip (right) is skipped. The left trips collect index 3 (length 60, distance 2) and then index 1 (length 50, distance 6), which brings the total length to 110. The bird stops with the stick at index 0 still on the ground, after flying 8.