Maximize Types After Freeing Shelves
Updated
Problem statement
A storeroom is used to organize items stored in it on N shelves. Shelves are numbered from 0 to N-1. The K-th shelf is dedicated to items of only one type, denoted by a positive integer A[K].
Recently it was decided that it is necessary to free R consecutive shelves. Shelves cannot be reordered. What is the maximum number of types of items that can still be stored in the storeroom after freeing R consecutive shelves?
Given:
- an array A of N integers representing types of items stored on storeroom shelves.
- an integer R representing the number of consecutive shelves to be freed.
Returns the maximum number of different types of items that can be stored in the storeroom after freeing R consecutive shelves.
Function
maximizeTypesAfterFreeingShelves(A: int[], R: int) → intExamples
Example 1
A = [2, 1, 2, 3, 2, 3, 2]R = 3return = 3It can be achieved by freeing shelves 2, 3, and 4. The remaining shelves contain item types {1, 2, 3}.
Example 2
A = [2, 3, 1, 1, 2]R = 2return = 3All three types {2, 3, 1} can still be stored by freeing the last two shelves.
Example 3
A = [20, 10, 10, 10, 30, 20]R = 3return = 3It can be achieved by freeing the first three shelves. The remaining item types are {10, 30, 20}.
Example 4
A = [1, 100000, 1]R = 3return = 0All shelves need to be freed, leaving no items.
Constraints
- N is an integer within the range [1..100,000].
- R is an integer within the range [1..N].
- Each element of array A is an integer within the range [1..100,000].
More Microsoft interview questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Authentication SystemOA · Seen Nov 2026
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Maximum String OperationsOA · Seen Nov 2026
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Perfect SubstringAXQ Capital · OA
- Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Get Message StatusSalesforce · OASalesforce interview questions
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Count Redundant File BackupsGoldman Sachs · OAGoldman Sachs interview questions
- Count Subarrays with at Least K Equal-Fruit PairsTikTok · OATikTok interview questions
- Accept or Decline QueriesRamp · OARamp interview questions
- Consistent LogsRippling · OARippling interview questions
- Longest Distinct StringSuperhuman · OASuperhuman interview questions
- Longest Substring Without Repeating CharactersAkuna Capital · OAAkuna Capital interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)