Count subarrays of ride values that contain exactly k prime numbers

Quick Overview

Count the contiguous subarrays of an integer array of ride values that contain exactly k prime numbers. Tests turning a counting problem into subarray sums over a 0/1 array, prefix sum frequency counting, and fast primality checks with a sieve.

Count subarrays of ride values that contain exactly k prime numbers

Company: Postmates

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `ride_values` and an integer `target_premiums`, return the total number of contiguous subarrays that contain exactly `target_premiums` prime numbers. A ride is considered premium if its value is a prime number. Count every contiguous subarray of `ride_values` whose number of premium rides equals `target_premiums`. ### Function Signature ```python def count_profitable_segments(ride_values: list[int], target_premiums: int) -> int: ``` ### Rules - A prime is an integer greater than 1 whose only positive divisors are 1 and itself. The value 1 is not prime. - Subarrays are identified by their start and end positions, so two subarrays with the same values at different positions are counted separately. - Only non-empty subarrays count. - The answer can be larger than `2^31 - 1` (for example, `10^5` non-prime values with `target_premiums = 0`). ### Constraints - `1 <= len(ride_values) <= 10^5` - `1 <= ride_values[i] <= 10^5` - `0 <= target_premiums <= len(ride_values)` ### Examples ```text Input: ride_values = [2, 3, 4, 5, 6], target_premiums = 2 Output: 4 ``` The primes are 2, 3 and 5 at indices 0, 1 and 3. The subarrays with exactly two of them are `[2, 3]` (indices 0 to 1), `[2, 3, 4]` (0 to 2), `[3, 4, 5]` (1 to 3) and `[3, 4, 5, 6]` (1 to 4). ```text Input: ride_values = [1, 4, 6, 8, 9], target_premiums = 0 Output: 15 ``` None of the values is prime, so every contiguous subarray has 0 primes. An array of length 5 has `5 * 6 / 2 = 15` subarrays. ```text Input: ride_values = [7, 1, 7], target_premiums = 1 Output: 4 ``` The subarrays with exactly one prime are `[7]` at index 0, `[7, 1]`, `[1, 7]` and `[7]` at index 2. The two single `[7]` subarrays sit at different positions, so both count. `[1]` has no prime and `[7, 1, 7]` has two.

Overview: Count the contiguous subarrays of an integer array of ride values that contain exactly k prime numbers. Tests turning a counting problem into subarray sums over a 0/1 array, prefix sum frequency counting, and fast primality checks with a sieve.

Postmates logo
Postmates
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array ride_values and an integer target_premiums, return the total number of contiguous subarrays that contain exactly target_premiums prime numbers.

A ride is considered premium if its value is a prime number. Count every contiguous subarray of ride_values whose number of premium rides equals target_premiums.

Function Signature

def count_profitable_segments(ride_values: list[int], target_premiums: int) -> int:

Rules

  • A prime is an integer greater than 1 whose only positive divisors are 1 and itself. The value 1 is not prime.
  • Subarrays are identified by their start and end positions, so two subarrays with the same values at different positions are counted separately.
  • Only non-empty subarrays count.
  • The answer can be larger than 2^31 - 1 (for example, 10^5 non-prime values with target_premiums = 0 ).

Constraints

  • 1 <= len(ride_values) <= 10^5
  • 1 <= ride_values[i] <= 10^5
  • 0 <= target_premiums <= len(ride_values)

Examples

Input: ride_values = [2, 3, 4, 5, 6], target_premiums = 2
Output: 4

The primes are 2, 3 and 5 at indices 0, 1 and 3. The subarrays with exactly two of them are [2, 3] (indices 0 to 1), [2, 3, 4] (0 to 2), [3, 4, 5] (1 to 3) and [3, 4, 5, 6] (1 to 4).

Input: ride_values = [1, 4, 6, 8, 9], target_premiums = 0
Output: 15

None of the values is prime, so every contiguous subarray has 0 primes. An array of length 5 has 5 * 6 / 2 = 15 subarrays.

Input: ride_values = [7, 1, 7], target_premiums = 1
Output: 4

The subarrays with exactly one prime are [7] at index 0, [7, 1], [1, 7] and [7] at index 2. The two single [7] subarrays sit at different positions, so both count. [1] has no prime and [7, 1, 7] has two.