Skip to content

Commit d61089d

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Frequency Queries. Coverage increased.
1 parent 77c2c8b commit d61089d

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/FrequencyQueries.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ static List<Integer> freqQuery(List<List<Integer>> queries) {
9696
result.add(freqMap.containsKey(value) ? __FOUND__ : __NOT_FOUND__);
9797
break;
9898
default:
99-
break;
99+
throw new IllegalArgumentException(
100+
"Operation %d not supported".formatted(operation));
100101
}
101102
}
102103

algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/FrequencyQueriesTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
46

57
import java.io.IOException;
68
import java.util.List;
@@ -21,6 +23,8 @@ public static class FrequencyQueriesTestCase {
2123

2224
List<FrequencyQueriesTestCase> testCases;
2325
List<FrequencyQueriesTestCase> testCase6;
26+
List<FrequencyQueriesTestCase> testCaseBorderCases;
27+
List<FrequencyQueriesTestCase> testCaseBorderCaseException;
2428

2529
@BeforeAll
2630
public void setup() throws IOException {
@@ -39,6 +43,20 @@ public void setup() throws IOException {
3943
"dictionaries_and_hashmaps",
4044
"frequency_queries.testcase6.json");
4145
this.testCase6 = JsonLoader.loadJson(path, FrequencyQueriesTestCase.class);
46+
47+
path = String.join("/",
48+
"hackerrank",
49+
"interview_preparation_kit",
50+
"dictionaries_and_hashmaps",
51+
"frequency_queries.testcase_border_cases.json");
52+
this.testCaseBorderCases = JsonLoader.loadJson(path, FrequencyQueriesTestCase.class);
53+
54+
path = String.join("/",
55+
"hackerrank",
56+
"interview_preparation_kit",
57+
"dictionaries_and_hashmaps",
58+
"frequency_queries.testcase_border_case_exception.json");
59+
this.testCaseBorderCaseException = JsonLoader.loadJson(path, FrequencyQueriesTestCase.class);
4260
}
4361

4462
@Test
@@ -71,4 +89,34 @@ void testFrequencyQueriesBigCases() {
7189
test.expected));
7290
}
7391
}
92+
93+
@Test
94+
void testFrequencyQueriesBorderCases() {
95+
List<Integer> solutionFound;
96+
97+
for (FrequencyQueriesTestCase test : testCaseBorderCases) {
98+
99+
solutionFound = FrequencyQueries.freqQuery(test.input);
100+
101+
assertEquals(test.expected, solutionFound,
102+
"%s(%s) answer must be: %s".formatted(
103+
"FrequencyQueriesTest.freqQuery",
104+
test.input,
105+
test.expected));
106+
}
107+
}
108+
109+
@Test
110+
void testFrequencyQueriesBorderCaseException() {
111+
for (FrequencyQueriesTestCase test : testCaseBorderCaseException) {
112+
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
113+
FrequencyQueries.freqQuery(test.input);
114+
});
115+
116+
String expectedMessage = "Operation 4 not supported";
117+
String actualMessage = exception.getMessage();
118+
119+
assertTrue(actualMessage.contains(expectedMessage));
120+
}
121+
}
74122
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"title": "Test Case Border Case when operation doesn't exists",
4+
"input": [
5+
[4, 1]
6+
],
7+
"expected": []
8+
}
9+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[
2+
{
3+
"title": "Test Case Border Case when decrease in frequency already exists",
4+
"input": [
5+
[1, 1],
6+
[1, 1],
7+
[1, 1],
8+
[2, 1],
9+
[1, 2],
10+
[1, 2],
11+
[1, 2],
12+
[2, 2],
13+
[3, 1],
14+
[3, 2],
15+
[3, 3]
16+
],
17+
"expected": [0, 1, 0]
18+
}
19+
]

0 commit comments

Comments
 (0)