Skip to content

Commit 0ff207a

Browse files
authored
Merge pull request #400 from sir-gon/feature/frequency-queries
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: F…
2 parents 68e938f + b41b6d6 commit 0ff207a

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# [Dictionaries and Hashmaps: Frequency Queries](https://www.hackerrank.com/challenges/frequency-queries)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingIntermediate`
5+
6+
You are given queries. Each query is of the form two integers described below:
7+
8+
- `1 x`: Insert x in your data structure.
9+
- `2 y`: Delete one occurence of y from your data structure, if present.
10+
- `3 z`: Check if any integer is present whose frequency is exactly `z`.
11+
If yes, print `1` else `0`.
12+
13+
The queries are given in the form of a 2-D array `queries` of
14+
size where `queries[i][0]` contains the operation,
15+
and `queries[i][0]` contains the data element.
16+
17+
## Example
18+
19+
The results of each operation are:
20+
21+
```text
22+
Operation Array Output
23+
(1,1) [1]
24+
(2,2) [1]
25+
(3,2) 0
26+
(1,1) [1,1]
27+
(1,1) [1,1,1]
28+
(2,1) [1,1]
29+
(3,2) 1
30+
```
31+
32+
Return an array with the output: [0, 1].
33+
34+
## Function Description
35+
36+
Complete the freqQuery function in the editor below.
37+
38+
freqQuery has the following parameter(s):
39+
40+
- `int queries[q][2]`: a 2-d array of integers
41+
42+
## Returns
43+
44+
- `int[]`: the results of queries of type `3`
45+
46+
## Input Format
47+
48+
The first line contains of an integer `q`, the number of queries.
49+
50+
Each of the next `q` lines contains two space-separated integers,
51+
`queries[i][0]` and `queries[i][1]`.
52+
53+
## Constraints
54+
55+
- $ 1 \leq q \leq 10^5 $
56+
- $ 1 \leq x, y, z \leq 10^9 $
57+
- All $ queries[i][0] \isin \{1, 2, 3\} $
58+
- $ 1 \leq queries[i][1] \leq 10^9 $
59+
60+
## Sample Input 0
61+
62+
```text
63+
8
64+
1 5
65+
1 6
66+
3 2
67+
1 10
68+
1 10
69+
1 6
70+
2 5
71+
3 2
72+
```
73+
74+
## Sample Output 0
75+
76+
```text
77+
0
78+
1
79+
```
80+
81+
## Explanation 0
82+
83+
For the first query of type `3`, there is no integer
84+
whose frequency is `2` (`array = [5, 6]`).
85+
So answer is `0`.
86+
87+
For the second query of type `3`, there are two integers
88+
in `array = [6, 10, 10, 6]` whose frequency is `2`(integer = `6` and `10`).
89+
So, the answer is `1`.
90+
91+
## Sample Input 1
92+
93+
```†ext
94+
4
95+
3 4
96+
2 1003
97+
1 16
98+
3 1
99+
```
100+
101+
## Sample Output 1
102+
103+
```†ext
104+
0
105+
1
106+
```
107+
108+
## Explanation 1
109+
110+
For the first query of type `3`, there is no integer of frequency `4`.
111+
The answer is `0`. For the second query of type `3`,
112+
there is one integer, `16` of frequency `1` so the answer is `1`.
113+
114+
## Sample Input 2
115+
116+
```text
117+
10
118+
1 3
119+
2 3
120+
3 2
121+
1 4
122+
1 5
123+
1 5
124+
1 4
125+
3 2
126+
2 4
127+
3 2
128+
```
129+
130+
## Sample Output 2
131+
132+
```text
133+
0
134+
1
135+
1
136+
137+
```
138+
139+
## Explanation 2
140+
141+
When the first output query is run, the array is empty.
142+
We insert two `4`'s and two `5`'s before the second output query,
143+
`arr = [4, 5, 5, 4]` so there are two instances of elements occurring twice.
144+
We delete a `4` and run the same query.
145+
Now only the instances of `5` satisfy the query.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger';
3+
4+
import { freqQuery } from './frequency_queries';
5+
6+
const TEST_CASES = [
7+
{
8+
title: 'Sample Test Case 0',
9+
input: [
10+
[1, 5],
11+
[1, 6],
12+
[3, 2],
13+
[1, 10],
14+
[1, 10],
15+
[1, 6],
16+
[2, 5],
17+
[3, 2]
18+
],
19+
expected: [0, 1]
20+
}
21+
];
22+
23+
describe('frequency_queries', () => {
24+
it('freqQuery test cases', () => {
25+
expect.assertions(1);
26+
27+
TEST_CASES.forEach((value) => {
28+
const answer = freqQuery(value.input);
29+
30+
console.debug(`checkMagazine(${value.input}) solution found: ${answer}`);
31+
32+
expect(answer).toStrictEqual(value.expected);
33+
});
34+
});
35+
36+
it('freqQuery border case', () => {
37+
expect.assertions(1);
38+
39+
expect(() => {
40+
freqQuery([[4, 1]]);
41+
}).toThrow('Invalid operation');
42+
});
43+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md]]
3+
*/
4+
5+
// Complete the freqQuery function below.
6+
export function freqQuery(queries: number[][]): number[] {
7+
const result: number[] = [];
8+
const data_map: Record<number, number> = {};
9+
10+
const __INITIAL__ = 0;
11+
const __INSERT__ = 1;
12+
const __DELETE__ = 2;
13+
const __SELECT__ = 3;
14+
15+
queries.forEach((query) => {
16+
const [operation, data] = query;
17+
18+
const current = data_map?.[data] ?? __INITIAL__;
19+
20+
switch (operation) {
21+
case __INSERT__:
22+
data_map[data] = current + 1;
23+
break;
24+
case __DELETE__:
25+
data_map[data] = Math.max(0, current - 1);
26+
break;
27+
case __SELECT__:
28+
for (const [key, value] of Object.entries(data_map)) {
29+
console.log(key, value);
30+
if (value == data) {
31+
result.push(1);
32+
break;
33+
}
34+
}
35+
if (result.length == 0) {
36+
result.push(0);
37+
}
38+
break;
39+
default:
40+
throw new Error('Invalid operation');
41+
}
42+
});
43+
44+
return result;
45+
}
46+
47+
export default { freqQuery };

0 commit comments

Comments
 (0)