Skip to content

Commit 052c8cb

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Frequency Queries. WIP
1 parent 34164f1 commit 052c8cb

File tree

7 files changed

+5497
-0
lines changed

7 files changed

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

0 commit comments

Comments
 (0)