Answer suffix maximum-frequency queries
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
You are given:
- An integer array `nums` of length `n`.
- An integer array `queries` of length `m`, where each `queries[k]` is an index `i` (0-based) into `nums`.
For each query index `i`, consider the suffix subarray `nums[i..n-1]`.
- Let `mx` be the maximum value in that suffix.
- Return how many times `mx` occurs in that suffix.
### Example
- `nums = [7, 5, 7, 2, 7]`
- `queries = [0, 1, 2, 3, 4]`
Suffixes and answers:
- `i=0`: `[7,5,7,2,7]` → max=7 occurs 3 → 3
- `i=1`: `[5,7,2,7]` → max=7 occurs 2 → 2
- `i=2`: `[7,2,7]` → max=7 occurs 2 → 2
- `i=3`: `[2,7]` → max=7 occurs 1 → 1
- `i=4`: `[7]` → max=7 occurs 1 → 1
Output: `[3, 2, 2, 1, 1]`
### Requirements
Design an algorithm that answers all queries efficiently (faster than scanning the suffix for each query).
### Constraints (assume typical interview-scale)
You may assume `n, m` can be up to ~200,000.
Quick Answer: This question evaluates algorithm design skills and array-processing competency for answering repeated queries efficiently, focusing on time and space complexity with large input sizes.