Compute total revenue from greedy VM rentals
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of greedy allocation strategies and the ability to maintain dynamic summaries (tracking current maximum and smallest positive inventories) under sequential state updates, reflecting algorithmic thinking and data-structure manipulation.
Part 1: Compute Total Revenue from Greedy VM Rentals
Constraints
- 1 <= len(stocks) <= 2 * 10^5
- 0 <= stocks[i] <= 10^9
- 0 <= m <= 2 * 10^5
- The answer may exceed 32-bit integer range.
Examples
Input: ([4, 2, 1], 4)
Expected Output:
Explanation: The revenues are 5, 4, 3, and 3, for a total of 15.
Input: ([3, 3], 3)
Expected Output:
Explanation: The revenues are 6, 5, and 4.
Input: ([0, 0, 0], 5)
Expected Output:
Explanation: No VM is available for the first customer, so the process stops immediately.
Input: ([1], 3)
Expected Output:
Explanation: Only one rental is possible. Its revenue is 1 + 1 = 2.
Input: ([1, 1, 0], 2)
Expected Output:
Explanation: Zero-stock types are ignored when computing the minimum positive stock. The two revenues are 2 and 2.
Hints
- You need to repeatedly know both the current maximum stock and the current minimum positive stock.
- A max-heap and a min-heap with lazy deletion let you update one chosen stock in O(log n) per customer.
Part 2: Maximum Equal-Frequency Blocks for Every Prefix
Constraints
- 0 <= len(s) <= 3000
- s contains only characters 'A' to 'Z'
Examples
Input: "ABBA"
Expected Output:
Explanation: Only the full prefix 'ABBA' can be split into 2 valid blocks: 'AB' and 'BA'.
Input: "AABB"
Expected Output:
Explanation: The prefix 'AA' can be split into 'A' and 'A', but 'AABB' cannot be split into 2 valid blocks because 'AA' and 'BB' have different frequencies.
Input: "AAAA"
Expected Output:
Explanation: Every prefix of length i can be split into i single-character blocks, all with the same frequencies.
Input: ""
Expected Output:
Explanation: There are no prefixes in an empty string.
Input: "ABABAB"
Expected Output:
Explanation: The full prefix can be split into 3 blocks: 'AB', 'AB', 'AB'.
Hints
- If a prefix is split into k valid blocks, then the total count of every character in that prefix must be divisible by k. So k must divide the gcd of the character counts.
- Compare blocks by their frequency signature, not by the raw substring. A prefix-frequency hash can make each block comparison O(1).