FastPrepShortest Window with Required Counts
Problems/Shortest Window with Required Counts

Shortest Window with Required Counts

Oracle logoOracle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Oracle Health asked this in the second round of a four-round onsite. The coding task was to find the shortest stretch of neighboring positions in a sequence that holds each of several chosen values at least a stated number of times. The candidate solved it with a sliding window.

The report did not give the input shape, what to return, or limits. We chose these: the requirements come as two matching arrays, one of distinct target values and one of minimum counts. The answer is the window's length, or -1 when no window works. Values are integers from -10^9 to 10^9. Limits are up to 2 x 10^4 items and 50 requirements.

Our version is about 85% the same as the reported question.

You are given an integer array nums and a list of requirements: value targets[j] must appear at least counts[j] times.

Return the length of the shortest contiguous subarray of nums that meets every requirement at once. If no subarray does, return -1. Values not in targets may appear in the subarray freely.

Function

shortestWindow(nums: int[], targets: int[], counts: int[]) → int

Examples

Example 1

nums = [1,2,2,3,1,2,3]targets = [2,3]counts = [2,1]return = 3
  • The window [2, 2, 3] (indices 1 to 3) holds two 2s and one 3.
  • No window of length 2 can hold three required items, so the answer is 3.

Example 2

nums = [5,1,5]targets = [5]counts = [3]return = -1
  • Only two 5s exist, so no window works: -1.

Example 3

nums = [4,9,4]targets = [9,4]counts = [1,1]return = 2
  • [4, 9] or [9, 4] both work, so the shortest length is 2.

Constraints

  • 1 <= nums.length <= 2 × 104
  • 1 <= targets.length == counts.length <= 50
  • The values in targets are distinct.
  • -109 <= nums[i], targets[j] <= 109
  • 1 <= counts[j] <= 2 × 104

More Oracle Onsite Interview questions

  • Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
  • Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
  • Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
  • Design Authentication ManagerONSITE INTERVIEW · Seen Oct 2026
  • Maximum Bouquet ValueONSITE INTERVIEW · Seen Oct 2026
  • Cache an Expensive Service Across ThreadsONSITE INTERVIEW · Seen Oct 2026
  • Island Count and Maximum AreaONSITE INTERVIEW · Seen Oct 2026
  • Largest Island After One FlipONSITE INTERVIEW · Seen Oct 2026
  • Minimum Coins with Reusable or Single-Use EntriesONSITE INTERVIEW · Seen Oct 2026
  • Minimum Meeting RoomsONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Count Zero Request ServersSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Top K Best-Selling Products in the Last N DaysAmazon · ONSITE INTERVIEW
  • Longest Binary Window with at Most K OnesLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Count Hotspot Playback WindowsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Count Equal Code Pairs Within Distance KInfosys · ONSITE INTERVIEWInfosys interview questions
  • Longest Subarray with At Most Two Distinct ValuesMoody's Investors Service · ONSITE INTERVIEW
  • Maximum Sum of a Unique-Element SubarrayTekion · ONSITE INTERVIEWTekion interview questions
  • Sliding Window: Target Containment and Most-Repeated WindowRoblox · ONSITE INTERVIEWRoblox interview questions
  • Invalid Transactions in Time OrderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
  • Subarray with a Given SumExpedia · ONSITE INTERVIEWExpedia interview questions

Interview reports that mention this question

  • Oracle SWE Onsite Interview Experience: 1 Experience
All Oracle interview questionsSee Oracle hiring insights
Saved
class Solution {
    public int shortestWindow(int[] nums, int[] targets, int[] counts) {
        // Write your code here.
    }
}
Checking account…