Array With Set All
Updated
Problem statement
Design an array of n integers, indexed from 0 to n - 1, where every element starts at 0. It supports three operations, and each should take O(1) time:
"set i v": store the valuevat indexi."get i": answer the current value at indexi."setAll v": change every element tov.
Process operations in order and return the answers of all get operations, in order.
Function
processArray(n: int, operations: String[]) → int[]Examples
Example 1
n = 3operations = ["set 0 5","get 0","get 2","setAll 7","get 0","set 1 4","get 1","get 2"]return = [5,0,7,4,7]Index 0 is set to 5, so get 0 is 5, and index 2 was never written, so get 2 is 0. After setAll 7 every element is 7, so get 0 is 7. Index 1 is then set to 4: get 1 is 4, while get 2 is still 7.
Example 2
n = 2operations = ["set 1 -3","setAll 9","setAll -2","get 1","set 0 6","setAll 1","get 0"]return = [-2,1]The later setAll -2 replaces the earlier setAll 9 and the value set at index 1, so get 1 is -2. Index 0 is set to 6, but the setAll 1 after it overwrites it, so get 0 is 1.
Constraints
1 <= n <= 10^5and1 <= operations.length <= 10^4- Each operation is
"set i v","get i"or"setAll v", with integers written without leading zeros or a plus sign. 0 <= i < nand-10^9 <= v <= 10^9
More Microsoft Onsite Interview questions
- 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
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Escape Room LeaderboardPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Insert, Delete, And Get Random In Constant TimeAmazon · ONSITE INTERVIEW
- Insert, Delete, And Get Random In Constant TimeGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Future-Effective Employee SalaryRippling · ONSITE INTERVIEWRippling interview questions
- Insert, Delete, and Get Random in Constant TimeBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Randomized Collection with Duplicate ValuesOkta · ONSITE INTERVIEWOkta interview questions
- Logger Rate LimiterAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Insert Delete GetRandom O(1)Charta Health · PHONE SCREENCharta Health interview questions
- Portfolio Tickers with Constant-Time Random PickCitadel · PHONE SCREENCitadel interview questions