FastPrepShortest Subarray With a Target Remainder
Problems/Shortest Subarray With a Target Remainder

Shortest Subarray With a Target Remainder

Google logoGoogle● MediumINTERNPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives a fixed modulus, a rule for negative remainders, a follow-up for the shortest length, and an O(70001) streaming idea. It does not give a function signature, a result for no match, or limits on the element values. Our version is about 96% the same as the reported question.

You are given an integer array nums and a target remainder k. The modulus is the fixed constant 70001.

Return the length of the shortest nonempty contiguous subarray whose element sum has remainder k modulo 70001. Return -1 if no such subarray exists.

Values may be negative. Treat every remainder canonically in the range 0 through 70000.

Process the values from left to right using O(70001) auxiliary space rather than storing a prefix record for every array position.

Function

shortestSubarrayRemainder(nums: int[], k: int) → int

Examples

Example 1

nums = [2,3,1,2]k = 6return = 3

Both [2,3,1] and [3,1,2] have sum 6. No qualifying subarray has length 1 or 2, so the shortest length is 3.

Example 2

nums = [5,-2,4,-6,3]k = 2return = 2

The subarray [-2,4] has sum 2. No single value has remainder 2, so the answer is 2.

Example 3

nums = [70000,1,70001]k = 0return = 1

The one-element subarray [70001] has remainder 0, so no longer qualifying subarray can improve the answer.

Example 4

nums = [2,4,8]k = 7return = -1

No nonempty contiguous subarray has a sum congruent to 7 modulo 70001.

Constraints

  • 1 <= nums.length <= 10^6.
  • -10^9 <= nums[i] <= 10^9.
  • 0 <= k < 70001.
  • The answer must use a nonempty contiguous subarray.
  • Use O(nums.length) time and O(70001) auxiliary space.

More Google Phone Screen questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
  • Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • Course Schedule Cycle DetectionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Logger Rate Limiter with Every-Arrival HistoryPHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Subarray Sum Equals KServiceNow · ONSITE INTERVIEWServiceNow interview questions
  • Count Subarrays with Equal Even and Odd CountsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Maximum Sum Desirable SubarrayGroww · PHONE SCREEN
  • Maximum Size Subarray Sum Equals KeBay · PHONE SCREENeBay interview questions
  • Count Subarrays with Sum KMakeMyTrip.com · ONSITE INTERVIEWMakeMyTrip.com interview questions
  • Detect a Contiguous Subarray with Target SumMeta · PHONE SCREENMeta interview questions
  • Count Subarrays With Target SumAmazon · PHONE SCREEN
  • Count Subarrays with Sum KByteDance · PHONE SCREENByteDance interview questions
  • Subarray Sum Equals KApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Longest Equal-Zero-One SubarrayMotive · PHONE SCREEN · ONSITE INTERVIEWMotive interview questions

Interview reports that mention this question

  • Google Intern Prefix Sums and Heaps: 6 Experiences (Part 3 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int shortestSubarrayRemainder(int[] nums, int k) {
      // Write your code here.
    }
}
Checking account…