Skip to content

Commit 307f4a3

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Frequency Queries. New solution notes document added.
1 parent 6d0dc84 commit 307f4a3

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# [Dictionaries and Hashmaps: Frequency Queries](https://www.hackerrank.com/challenges/frequency-queries)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingIntermediate` `#DictionariesAndHashmaps`
5+
6+
## First solution
7+
8+
The first solution is based on the idea of ​​storing the values
9+
​​of insert and delete operations in a dictionary.
10+
11+
For the "select" operation, a search loop is made to find the expected frequency,
12+
which in the best case is cut off when the value is found,
13+
but in the worst case it goes through the entire dictionary.
14+
15+
## Optimized solution
16+
17+
The optimized solution tries to reduce the worst case problem above,
18+
reducing the select operation to direct access to the expected frequency.
19+
20+
To achieve this, it is necessary to maintain an "inverted" dictionary
21+
where the frequency values ​​are the keys and the values ​​of each element
22+
are stored in a list of values ​​that have the same frequency.
23+
24+
To maintain this structure, any operation that alters the data (insert, delete),
25+
must update "in which frequency group" the element will be.
26+
27+
This solution transfers the cost of the search to the data update operations.

docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# [Dictionaries and Hashmaps: Frequency Queries](https://www.hackerrank.com/challenges/frequency-queries)
22

33
- Difficulty: `#medium`
4-
- Category: `#ProblemSolvingIntermediate`
4+
- Category: `#ProblemSolvingIntermediate` `#DictionariesAndHashmaps`
55

66
You are given queries. Each query is of the form two integers described below:
77

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency_queries_bruteforce.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md]]
3+
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries-solution-notes.md]]
34
*/
45

56
export function freqQuery(queries) {

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency_queries_optimized.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md]]
3+
* @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries-solution-notes.md]]
34
*/
45

56
export function updateFrequency(frequencyMap, data, currentFreq, newFreq) {

0 commit comments

Comments
 (0)