Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Arrays Algorithm Puzzle

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

Your question is Arrays Algorithm Puzzle. 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

Teamware Solutions' QA analytics pipeline records signed event-impact scores from a test execution stream. Given the scores and a required threshold, find the length of the shortest contiguous segment whose sum is at least the threshold.

Scores may be negative, so a sliding-window approach that assumes expanding the window always increases its sum is not valid. Return -1 if no qualifying segment exists.

Formal Specification

Implement shortest_subarray(nums, k), where nums is a list of integers and k is an integer threshold. Return an integer representing the minimum length of a non-empty contiguous subarray with sum greater than or equal to k.

Constraints

  • 1 <= len(nums) <= 100,000
  • -1,000,000,000 <= nums[i] <= 1,000,000,000
  • 1 <= k <= 1,000,000,000,000
  • The answer must be computed in O(n) time

Function Signature

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