Skip to content

Commit 28a80d4

Browse files
authored
Merge pull request #199 from sir-gon/feature/frequency_queries
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: F…
2 parents 34164f1 + 5a9f5ca commit 28a80d4

File tree

7 files changed

+5496
-0
lines changed

7 files changed

+5496
-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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_Dictionarys/frequency-queries.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Collections.Generic;
7+
8+
public class FrequencyQueries
9+
{
10+
[ExcludeFromCodeCoverage]
11+
private FrequencyQueries() { }
12+
13+
private static readonly long __INITIAL__ = 1L;
14+
15+
private const int __INSERT__ = 1;
16+
private const int __DELETE__ = 2;
17+
private const int __SELECT__ = 3;
18+
19+
private static readonly int __NOT_FOUND__ = 0;
20+
private static readonly int __FOUND__ = 1;
21+
22+
readonly Dictionary<long, long> valueFreqs = [];
23+
readonly Dictionary<long, List<long>> freqDictionary = [];
24+
List<int> result = [];
25+
26+
void insert(long value)
27+
{
28+
bool currentValueFreqCountExists = valueFreqs.TryGetValue(value, out long currentValueFreqCount);
29+
30+
long newFreqCount;
31+
32+
newFreqCount = !currentValueFreqCountExists ? __INITIAL__ : currentValueFreqCount + 1;
33+
valueFreqs[value] = newFreqCount;
34+
35+
freqDictionary.TryGetValue(newFreqCount, out List<long>? newFreq);
36+
37+
// delete current frequency
38+
if (currentValueFreqCountExists)
39+
{
40+
freqDictionary[currentValueFreqCount].Remove(value);
41+
if (freqDictionary[currentValueFreqCount].Count == 0)
42+
{
43+
freqDictionary.Remove(currentValueFreqCount);
44+
}
45+
}
46+
47+
// add new frequency
48+
if (newFreq == null)
49+
{
50+
newFreq = [];
51+
newFreq.Add(value);
52+
freqDictionary[newFreqCount] = newFreq;
53+
}
54+
else
55+
{
56+
freqDictionary[newFreqCount].Add(value);
57+
}
58+
}
59+
60+
void delete(long value)
61+
{
62+
bool currentValueFreqCountExists = valueFreqs.TryGetValue(value, out long currentValueFreqCount);
63+
64+
long newFreqCount;
65+
66+
newFreqCount = !currentValueFreqCountExists ? 0 : currentValueFreqCount - 1;
67+
if (newFreqCount > 0)
68+
{
69+
valueFreqs[value] = newFreqCount;
70+
71+
freqDictionary.TryGetValue(newFreqCount, out List<long>? newFreq);
72+
73+
// add new frequency
74+
if (newFreq == null)
75+
{
76+
newFreq = [];
77+
newFreq.Add(value);
78+
freqDictionary[newFreqCount] = newFreq;
79+
}
80+
else
81+
{
82+
freqDictionary[newFreqCount].Add(value);
83+
}
84+
}
85+
else
86+
{
87+
valueFreqs.Remove(value);
88+
}
89+
90+
// delete current frequency
91+
if (currentValueFreqCountExists)
92+
{
93+
freqDictionary[currentValueFreqCount].Remove(value);
94+
if (freqDictionary[currentValueFreqCount].Count == 0)
95+
{
96+
freqDictionary.Remove(currentValueFreqCount);
97+
}
98+
}
99+
}
100+
101+
void reset()
102+
{
103+
result = [];
104+
}
105+
106+
void select(long value)
107+
{
108+
result.Add(freqDictionary.ContainsKey(value) ? __FOUND__ : __NOT_FOUND__);
109+
}
110+
111+
static FrequencyQueries factory()
112+
{
113+
FrequencyQueries fq = new();
114+
fq.reset();
115+
116+
return fq;
117+
}
118+
119+
/**
120+
* FrequencyQueries.
121+
*/
122+
public static List<int> freqQuery(List<List<int>> queries)
123+
{
124+
FrequencyQueries fq = factory();
125+
126+
foreach (List<int> query in queries)
127+
{
128+
int operation = query[0];
129+
long value = query[1];
130+
131+
switch (operation)
132+
{
133+
case __INSERT__:
134+
fq.insert(value);
135+
136+
break;
137+
case __DELETE__:
138+
fq.delete(value);
139+
140+
break;
141+
case __SELECT__:
142+
fq.select(value);
143+
break;
144+
default:
145+
throw new InvalidOperationException($"Operation {operation} not supported");
146+
}
147+
}
148+
149+
return fq.result;
150+
}
151+
}

0 commit comments

Comments
 (0)