Shortest Window with Required Counts
Updated
Problem statement
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[]) → intExamples
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 × 1041 <= targets.length == counts.length <= 50- The values in
targetsare distinct. -109 <= nums[i], targets[j] <= 1091 <= 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