Skip to content

Commit 5a51587

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Frequency Queries. Fixed result. Fails due time out in big cases.
1 parent 604e530 commit 5a51587

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency_queries.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md]]
33
*/
44

5-
// Complete the freqQuery function below.
65
export function freqQuery(queries) {
76
const result = [];
87
const dataMap = {};
@@ -12,6 +11,9 @@ export function freqQuery(queries) {
1211
const __DELETE__ = 2;
1312
const __SELECT__ = 3;
1413

14+
const __NOT_FOUND__ = 0;
15+
const __FOUND__ = 1;
16+
1517
queries.forEach((query) => {
1618
const [operation, data] = query;
1719

@@ -24,18 +26,23 @@ export function freqQuery(queries) {
2426
case __DELETE__:
2527
dataMap[data] = Math.max(0, current - 1);
2628
break;
27-
case __SELECT__:
28-
for (const [key, value] of Object.entries(dataMap)) {
29+
case __SELECT__: {
30+
let i = 0;
31+
const entries = Object.entries(dataMap);
32+
33+
for (const [key, value] of entries) {
2934
console.log(key, value);
3035
if (value === data) {
31-
result.push(1);
36+
result.push(__FOUND__);
3237
break;
3338
}
39+
i += 1;
3440
}
35-
if (result.length === 0) {
36-
result.push(0);
41+
if (i === entries.length) {
42+
result.push(__NOT_FOUND__);
3743
}
3844
break;
45+
}
3946
default:
4047
throw new Error('Invalid operation');
4148
}

0 commit comments

Comments
 (0)