Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Subarrays Product Less Than K

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Subarrays Product Less Than K. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

AlphaGrep Securities monitors contiguous runs of positive integer signals and needs to count how many have a combined product below a threshold. Given an array nums of positive integers and an integer k, return the number of contiguous, non-empty subarrays whose product is strictly less than k.

Formal Specification

Implement num_subarray_product_less_than_k(nums, k).

  • Input: nums, a list of positive integers, and k, a positive integer.
  • Output: An integer representing the number of non-empty contiguous subarrays whose product is less than k.
  • Each element must be used according to its position, and subarrays must be contiguous.

Constraints

  • 1 <= len(nums) <= 3 * 10^4
  • 1 <= nums[i] <= 1000
  • 0 <= k <= 10^9
  • Only non-empty contiguous subarrays are counted
  • All values are positive, which enables the sliding-window method

Function Signature

def num_subarray_product_less_than_k(nums, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output