PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Microsoft

Find max consecutive elements with sum below target

Last updated: Apr 26, 2026

Quick Overview

This question evaluates algorithmic problem-solving with arrays, including reasoning about contiguous subsequences, handling sorted inputs and edge cases, and analyzing time and space complexity.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Find max consecutive elements with sum below target

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given: - An integer array `nums` of length `n`, sorted in non-decreasing order. - An integer `index` such that `0 ≤ index < n`. - An integer `target`. Starting from position `index`, you may take a contiguous sequence of elements going to the right: `nums[index], nums[index + 1], ..., nums[index + k - 1]` for some integer `k ≥ 0`. You want the sum of the chosen elements to be **strictly less than** `target`: \[ \sum_{i=index}^{index + k - 1} nums[i] < target. \] Find the **maximum possible value of `k`** (the maximum number of consecutive elements starting from `index` whose sum is `< target`). If even `nums[index] ≥ target`, then the answer should be `0`. Design an efficient algorithm to compute this maximum `k`. You may assume: - `1 ≤ n` - `nums` is sorted in non-decreasing order. Describe the algorithm you would implement and its time and space complexity.

Quick Answer: This question evaluates algorithmic problem-solving with arrays, including reasoning about contiguous subsequences, handling sorted inputs and edge cases, and analyzing time and space complexity.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Nov 17, 2025, 12:00 AM
Software Engineer
Take-home Project
Coding & Algorithms
7
0

You are given:

  • An integer array nums of length n , sorted in non-decreasing order.
  • An integer index such that 0 ≤ index < n .
  • An integer target .

Starting from position index, you may take a contiguous sequence of elements going to the right:
nums[index], nums[index + 1], ..., nums[index + k - 1] for some integer k ≥ 0.

You want the sum of the chosen elements to be strictly less than target:

∑i=indexindex+k−1nums[i]<target.\sum_{i=index}^{index + k - 1} nums[i] < target.∑i=indexindex+k−1​nums[i]<target.

Find the maximum possible value of k (the maximum number of consecutive elements starting from index whose sum is < target). If even nums[index] ≥ target, then the answer should be 0.

Design an efficient algorithm to compute this maximum k.

You may assume:

  • 1 ≤ n
  • nums is sorted in non-decreasing order.

Describe the algorithm you would implement and its time and space complexity.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.