Stretches that hit a target

hashing

Stretches that hit a target

DoorDash Python Interview Question

DoorDash's operations team wants to know how many stretches of consecutive hours had exactly a target number of orders in total. The hourly numbers are adjustments, so some can be negative.

Write a function count_subarrays(orders_per_hour, target) that returns how many stretches of one or more consecutive hours add up to exactly the target.

Asked of

  • Data Analyst
  • Data Engineer
  • Data Scientist
  • ML Engineer
  • AI Engineer

Example 1

Input

orders_per_hour = [1, 2, 3], target = 3

Output

2

Example 2

Input

orders_per_hour = [1, 1, 1], target = 2

Output

2

Explanation

In the first example, the stretches [1, 2] and [3] both add up to 3, so the answer is 2. In the second example, [1, 1] appears twice, starting at the first and second hours.

Submit also runs 4 hidden test cases that check edge cases.