Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Longest Subarray With Condition
00:00
5 left

Longest Subarray With Condition

HardPython

Problem

CarWale processes a sequence of signed price adjustments for a vehicle listing. Given an integer array adjustments and an integer limit, find the longest contiguous subarray whose sum is less than or equal to limit.

Values may be positive, zero, or negative, so a standard sliding window is not sufficient. Return the inclusive [start, end] indices of the longest valid subarray. If multiple subarrays have the same maximum length, return the one with the smallest start index. Return [] if no non-empty subarray is valid.

Formal Specification

Implement longest_subarray_at_most(adjustments, limit).

  • Input: adjustments, a list of integers, and limit, an integer.
  • Output: A two-element list [start, end], or an empty list if no valid subarray exists.
  • A subarray from start through end is valid when sum(adjustments[start:end + 1]) <= limit.

Constraints

  • 1 <= len(adjustments) <= 2 * 10^5
  • -10^9 <= adjustments[i] <= 10^9
  • -10^14 <= limit <= 10^14

Function Signature

def longest_subarray_at_most(adjustments, limit):
Interviewer

Your question is Longest Subarray With Condition. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.