Range Frequency Queries - Problem
Design a Smart Frequency Counter Data Structure

Imagine you're building a data analysis tool that needs to quickly answer questions like: "How many times does the value 5 appear between positions 2 and 8 in this dataset?"

Your task is to implement the RangeFreqQuery class that can efficiently handle multiple frequency queries on subarrays:

Constructor: RangeFreqQuery(int[] arr) - Initialize with a 0-indexed integer array
Query Method: int query(int left, int right, int value) - Return how many times value appears in the subarray arr[left...right] (inclusive)

Example: If arr = [12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56]
query(1, 2, 4) returns 1 (value 4 appears once in subarray [33, 4])
query(0, 2, 33) returns 1 (value 33 appears once in subarray [12, 33, 4])
query(1, 3, 33) returns 1 (value 33 appears once in subarray [33, 4, 56])

Input & Output

example_1.py — Basic Usage
$ Input: arr = [12, 33, 4, 56, 22, 2, 34, 33, 22, 12, 34, 56] queries = [ query(1, 2, 4), query(0, 2, 33) ]
Output: [1, 1]
💡 Note: First query: In subarray [33, 4] (indices 1-2), value 4 appears 1 time. Second query: In subarray [12, 33, 4] (indices 0-2), value 33 appears 1 time.
example_2.py — Multiple Occurrences
$ Input: arr = [1, 2, 2, 1, 2, 3] queries = [ query(0, 5, 2), query(1, 4, 2), query(0, 1, 1) ]
Output: [3, 2, 1]
💡 Note: Value 2 appears 3 times in entire array [0,5], 2 times in range [1,4], and value 1 appears 1 time in range [0,1].
example_3.py — Edge Cases
$ Input: arr = [5] queries = [ query(0, 0, 5), query(0, 0, 1) ]
Output: [1, 0]
💡 Note: Single element array: value 5 found once at index 0, value 1 not found (returns 0).

Constraints

  • 1 ≤ arr.length ≤ 105
  • -104 ≤ arr[i] ≤ 104
  • 1 ≤ number of queries ≤ 104
  • 0 ≤ left ≤ right < arr.length
  • Time limit: 2 seconds per test case

Visualization

Tap to expand
📚 Smart Library Catalog SystemLibrary Shelves (Original Array)HPLRHPGOTShelf: 0 1 2 3 4 5 6Build CatalogSmart Index Catalog (Hash Map)Harry Potter (HP)Positions: [0, 2, 7, 11]Count in range [1,3]: 1Lord of Rings (LR)Positions: [1, 5, 9]Count in range [1,3]: 1Query: "HP books in shelves 1-3?"1. Look up HP card: [0, 2, 7, 11]2. Binary search: positions ≥ 1 and ≤ 33. Found: position 2 ✓4. Answer: 1 book
Understanding the Visualization
1
Build the Catalog
Create index cards for each book type, listing all shelf positions where they appear
2
Smart Lookup
When asked about a range, grab the card for that book type
3
Binary Search Magic
Use the sorted positions to quickly count how many fall within the requested range
Key Takeaway
🎯 Key Insight: Instead of repeatedly scanning data, preprocess it once into a searchable format. The hash map + binary search combination turns O(n) queries into O(log n) lookups!
Asked in
Google 42 Amazon 38 Microsoft 35 Meta 28
68.9K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen