Ctrl K

Back at the Dock

Easy
A cart rides along an endless straight rail and starts at the dock (point 0). The array moves lists its moves in order: a positive number means it travels that many metres to the right, a negative one means it travels the absolute value to the left. After each move we look at where the cart has stopped. Return how many times it stopped exactly at the dock. Passing the dock in the middle of a move does not count; only the stopping point matters. The starting position does not count either.
Example 1
moves=[4, -1, -3, 2, -2] → 2

Positions after each move: 4, 3, 0, 2, 0. The cart stops at the dock twice.

Example 2
moves=[3, -5, 4] → 0

Positions: 3, −2, 2. During the second move the cart rolls past the dock without stopping there, which does not count.

Constraints
  • 1 ≤ len(moves) ≤ 100
  • -10 ≤ moves[i] ≤ 10
  • moves[i] ≠ 0
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Shuttle Robot EasyShuttle Seat Plan MediumWho Refills the Coffee Beans MediumRunning Sum Waves MediumPump Rods at Their Peak Hard
loading Python…
Input
moves=[4, -1, -3, 2, -2]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 111 tests on the server, hidden ones included.