Skip to content

Commit e105b21

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Frequency Queries. Solved ✓.
1 parent 4435d6d commit e105b21

File tree

4 files changed

+324
-0
lines changed

4 files changed

+324
-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` `#dictionaries` `#hashmaps`
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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md]]
3+
*/
4+
5+
package hackerrank
6+
7+
const __INITIAL__ = 0
8+
9+
const __INSERT__ = 1
10+
const __DELETE__ = 2
11+
const __SELECT__ = 3
12+
13+
const __NOT_FOUND__ = 0
14+
const __FOUND__ = 1
15+
16+
func freqQuery(queries [][]int32) []int32 {
17+
m := make(map[int32]int32)
18+
freq := make(map[int32]int32)
19+
var result []int32
20+
21+
for _, query := range queries {
22+
switch query[0] {
23+
case __INSERT__:
24+
if m[query[1]] == __INITIAL__ {
25+
m[query[1]] = 1
26+
} else {
27+
freq[m[query[1]]]--
28+
m[query[1]]++
29+
}
30+
freq[m[query[1]]]++
31+
case __DELETE__:
32+
if m[query[1]] > __INITIAL__ {
33+
freq[m[query[1]]]--
34+
m[query[1]]--
35+
freq[m[query[1]]]++
36+
}
37+
case __SELECT__:
38+
if freq[query[1]] > __NOT_FOUND__ {
39+
result = append(result, __FOUND__)
40+
} else {
41+
result = append(result, __NOT_FOUND__)
42+
}
43+
}
44+
}
45+
46+
return result
47+
}
48+
49+
func FreqQuery(queries [][]int32) []int32 {
50+
return freqQuery(queries)
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
[
2+
{
3+
"title": "Sample Test Case 0",
4+
"input": [
5+
[1, 5],
6+
[1, 6],
7+
[3, 2],
8+
[1, 10],
9+
[1, 10],
10+
[1, 6],
11+
[2, 5],
12+
[3, 2]
13+
],
14+
"expected": [0, 1]
15+
},
16+
{
17+
"title": "Sample Test Case 2",
18+
"input": [
19+
[1, 3],
20+
[2, 3],
21+
[3, 2],
22+
[1, 4],
23+
[1, 5],
24+
[1, 5],
25+
[1, 4],
26+
[3, 2],
27+
[2, 4],
28+
[3, 2]
29+
],
30+
"expected": [0, 1, 1]
31+
},
32+
{
33+
"title": "Sample Test Case 3",
34+
"input": [
35+
[1, 3],
36+
[1, 38],
37+
[2, 1],
38+
[1, 16],
39+
[2, 1],
40+
[2, 2],
41+
[1, 64],
42+
[1, 84],
43+
[3, 1],
44+
[1, 100],
45+
[1, 10],
46+
[2, 2],
47+
[2, 1],
48+
[1, 67],
49+
[2, 2],
50+
[3, 1],
51+
[1, 99],
52+
[1, 32],
53+
[1, 58],
54+
[3, 2]
55+
],
56+
"expected": [1, 1, 0]
57+
},
58+
{
59+
"title": "Sample Test Case 3",
60+
"input": [
61+
[1, 3],
62+
[1, 38],
63+
[2, 1],
64+
[1, 16],
65+
[2, 1],
66+
[2, 2],
67+
[1, 64],
68+
[1, 84],
69+
[3, 1],
70+
[1, 100],
71+
[1, 10],
72+
[2, 2],
73+
[2, 1],
74+
[1, 67],
75+
[2, 2],
76+
[3, 1],
77+
[1, 99],
78+
[1, 32],
79+
[1, 58],
80+
[3, 2]
81+
],
82+
"expected": [1, 1, 0]
83+
}
84+
]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type FrequencyQueriesTestCase struct {
13+
Input [][]int32 `json:"input"`
14+
Expected []int32 `json:"expected"`
15+
}
16+
17+
var FrequencyQueriesTestCases []FrequencyQueriesTestCase
18+
19+
// You can use testing.T, if you want to test the code without benchmarking
20+
func FrequencyQueriesSetupSuite(t testing.TB) {
21+
wd, _ := os.Getwd()
22+
filepath := wd + "/frequency_queries.testcases.json"
23+
t.Log("Setup test cases from JSON: ", filepath)
24+
25+
var _, err = utils.LoadJSON(filepath, &FrequencyQueriesTestCases)
26+
if err != nil {
27+
t.Log(err)
28+
}
29+
}
30+
31+
func TestFrequencyQueries(t *testing.T) {
32+
33+
FrequencyQueriesSetupSuite(t)
34+
35+
for _, tt := range FrequencyQueriesTestCases {
36+
testname := fmt.Sprintf("FreqQuery(%v) => %v \n", tt.Input, tt.Expected)
37+
t.Run(testname, func(t *testing.T) {
38+
39+
ans := FreqQuery(tt.Input)
40+
assert.Equal(t, tt.Expected, ans)
41+
})
42+
43+
}
44+
}

0 commit comments

Comments
 (0)