Count and Enumerate Subarrays by Their Product

Quick Overview

Count subarrays with product below or equal to k using positive-integer sliding windows, then enumerate valid subarrays with honest output-sensitive complexity.

Count and Enumerate Subarrays by Their Product

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given an array of positive integers, count contiguous nonempty subarrays whose product is less than k. Then adapt the solution to count subarrays whose product equals k, and enumerate every subarray satisfying the original less-than condition. ### Constraints & Assumptions This is the standard positive-integer formulation of problem 713. Equal-valued subarrays at different positions are distinct occurrences. Use exact integer arithmetic; zero or negative elements would require a different argument. ### Clarifying Questions to Ask - Are all values strictly positive integers? - Does enumeration return index ranges or copied values? Here yield each subarray's values, preserving duplicate occurrences. ### Part 1 — Product less than k Count qualifying subarrays efficiently. #### What This Part Should Cover - A multiplicative sliding window and shrinking until its product is below k. - Counting all valid suffixes ending at the current right endpoint. - The k<=1 case for positive integer inputs. ### Part 2 — Product exactly k Return the number of subarrays with product equal to k. #### What This Part Should Cover - A correct exact-count construction, including repeated ones. - Why a direct single-window equality check misses multiple valid starts. ### Part 3 — Enumerate the original subarrays Output every subarray with product less than k. #### What This Part Should Cover - Enumerating starts within each valid window. - Output-sensitive cost, including copying the returned values. ```hint All suffixes of a valid positive window qualify Removing positive integer factors cannot increase its product. ``` ### What a Strong Answer Covers - One positivity-based invariant shared by counting and enumeration. - Accurate complexity that does not call large output generation linear. ### Follow-up Questions - What changes when zero values are allowed? - How would returning index ranges reduce enumeration memory and copying?

Overview: Count subarrays with product below or equal to k using positive-integer sliding windows, then enumerate valid subarrays with honest output-sensitive complexity.

Apple logo
Apple
Nov 11, 2026
easySoftware EngineerTechnical ScreenCoding & Algorithms
1
0

Given an array of positive integers, count contiguous nonempty subarrays whose product is less than k. Then adapt the solution to count subarrays whose product equals k, and enumerate every subarray satisfying the original less-than condition.

Constraints & Assumptions

This is the standard positive-integer formulation of problem 713. Equal-valued subarrays at different positions are distinct occurrences. Use exact integer arithmetic; zero or negative elements would require a different argument.

Clarifying Questions to Ask Guidance

  • Are all values strictly positive integers?
  • Does enumeration return index ranges or copied values? Here yield each subarray's values, preserving duplicate occurrences.

Part 1 — Product less than k

Count qualifying subarrays efficiently.

What This Part Should Cover Guidance

  • A multiplicative sliding window and shrinking until its product is below k.
  • Counting all valid suffixes ending at the current right endpoint.
  • The k<=1 case for positive integer inputs.

Part 2 — Product exactly k

Return the number of subarrays with product equal to k.

What This Part Should Cover Guidance

  • A correct exact-count construction, including repeated ones.
  • Why a direct single-window equality check misses multiple valid starts.

Part 3 — Enumerate the original subarrays

Output every subarray with product less than k.

What This Part Should Cover Guidance

  • Enumerating starts within each valid window.
  • Output-sensitive cost, including copying the returned values.

What a Strong Answer Covers Guidance

  • One positivity-based invariant shared by counting and enumeration.
  • Accurate complexity that does not call large output generation linear.

Follow-up Questions Guidance

  • What changes when zero values are allowed?
  • How would returning index ranges reduce enumeration memory and copying?