Skip to content

Feature/frequency queries #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* FrequencyQueries.
*
* @link Problem definition
* [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency-queries.md]]
*/
public class FrequencyQueries {
private FrequencyQueries() {
}

private static final long __INITIAL__ = 1L;

private static final int __INSERT__ = 1;
private static final int __DELETE__ = 2;
private static final int __SELECT__ = 3;

private static final int __NOT_FOUND__ = 0;
private static final int __FOUND__ = 1;

Map<Long, Long> valueFreqs = new HashMap<>();
Map<Long, List<Long>> freqMap = new HashMap<>();
List<Integer> result = new ArrayList<>();

void insert(Long value) {
Long currentFreqCount = this.valueFreqs.getOrDefault(value, null);
Long newFreqCount;
List<Long> newFreq;

newFreqCount = (currentFreqCount == null ? __INITIAL__ : currentFreqCount + 1L);
valueFreqs.put(value, newFreqCount);

newFreq = freqMap.getOrDefault(newFreqCount, null);

// delete current frequency
if (currentFreqCount != null) {
freqMap.get(currentFreqCount).remove(value);
if (freqMap.get(currentFreqCount).isEmpty()) {
freqMap.remove(currentFreqCount);
}
}

// add new frequency
if (newFreq == null) {
newFreq = new ArrayList<>();
newFreq.add(value);
freqMap.put(newFreqCount, newFreq);
} else {
freqMap.get(newFreqCount).add(value);
}
}

void delete(Long value) {
Long currentFreqCount = this.valueFreqs.getOrDefault(value, null);
Long newFreqCount;
List<Long> newFreq;

newFreqCount = (currentFreqCount == null ? 0 : currentFreqCount - 1L);
if (newFreqCount > 0L) {
valueFreqs.put(value, newFreqCount);

newFreq = freqMap.getOrDefault(newFreqCount, null);
// add new frequency
if (newFreq == null) {
newFreq = new ArrayList<>();
newFreq.add(value);
freqMap.put(newFreqCount, newFreq);
} else {
freqMap.get(newFreqCount).add(value);
}
} else {
valueFreqs.remove(value);
}

// delete current frequency
if (currentFreqCount != null) {
freqMap.get(currentFreqCount).remove(value);
if (freqMap.get(currentFreqCount).isEmpty()) {
freqMap.remove(currentFreqCount);
}
}
}

/**
* FrequencyQueries.
*/
static List<Integer> freqQuery(List<List<Integer>> queries) {

FrequencyQueries fq = new FrequencyQueries();
fq.result = new ArrayList<>();

for (List<Integer> query : queries) {
int operation = query.get(0);
long value = query.get(1);

switch (operation) {
case __INSERT__:
fq.insert(value);

break;
case __DELETE__:
fq.delete(value);

break;
case __SELECT__:
fq.result.add(fq.freqMap.containsKey(value) ? __FOUND__ : __NOT_FOUND__);
break;
default:
throw new IllegalArgumentException(
"Operation %d not supported".formatted(operation));
}
}

return fq.result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package ae.hackerrank.interview_preparation_kit.dictionaries_and_hashmaps;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.util.List;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import util.JsonLoader;

@TestInstance(Lifecycle.PER_CLASS)
class FrequencyQueriesTest {

public static class FrequencyQueriesTestCase {
public String title;
public List<List<Integer>> input;
public List<Integer> expected;
}

List<FrequencyQueriesTestCase> testCases;
List<FrequencyQueriesTestCase> testCase6;
List<FrequencyQueriesTestCase> testCaseBorderCases;
List<FrequencyQueriesTestCase> testCaseBorderCaseException;

@BeforeAll
void setup() throws IOException {
String path;
path = String.join("/",
"hackerrank",
"interview_preparation_kit",
"dictionaries_and_hashmaps",
"frequency_queries.testcases.json");

this.testCases = JsonLoader.loadJson(path, FrequencyQueriesTestCase.class);

path = String.join("/",
"hackerrank",
"interview_preparation_kit",
"dictionaries_and_hashmaps",
"frequency_queries.testcase6.json");
this.testCase6 = JsonLoader.loadJson(path, FrequencyQueriesTestCase.class);

path = String.join("/",
"hackerrank",
"interview_preparation_kit",
"dictionaries_and_hashmaps",
"frequency_queries.testcase_border_cases.json");
this.testCaseBorderCases = JsonLoader.loadJson(path, FrequencyQueriesTestCase.class);

path = String.join("/",
"hackerrank",
"interview_preparation_kit",
"dictionaries_and_hashmaps",
"frequency_queries.testcase_border_case_exception.json");
this.testCaseBorderCaseException = JsonLoader.loadJson(path, FrequencyQueriesTestCase.class);
}

@Test
void testFrequencyQueries() {
List<Integer> solutionFound;

for (FrequencyQueriesTestCase test : testCases) {
solutionFound = FrequencyQueries.freqQuery(test.input);

assertEquals(test.expected, solutionFound,
"%s(%s) answer must be: %s".formatted(
"FrequencyQueriesTest.freqQuery",
test.input,
test.expected));
}
}

@Test
void testFrequencyQueriesBigCases() {
List<Integer> solutionFound;

for (FrequencyQueriesTestCase test : testCase6) {

solutionFound = FrequencyQueries.freqQuery(test.input);

assertEquals(test.expected, solutionFound,
"%s(%s) answer must be: %s".formatted(
"FrequencyQueriesTest.freqQuery",
test.input,
test.expected));
}
}

@Test
void testFrequencyQueriesBorderCases() {
List<Integer> solutionFound;

for (FrequencyQueriesTestCase test : testCaseBorderCases) {

solutionFound = FrequencyQueries.freqQuery(test.input);

assertEquals(test.expected, solutionFound,
"%s(%s) answer must be: %s".formatted(
"FrequencyQueriesTest.freqQuery",
test.input,
test.expected));
}
}

@Test
void testFrequencyQueriesBorderCaseException() {
for (FrequencyQueriesTestCase test : testCaseBorderCaseException) {
Exception exception = assertThrows(IllegalArgumentException.class, () -> {
FrequencyQueries.freqQuery(test.input);
});

String expectedMessage = "Operation 4 not supported";
String actualMessage = exception.getMessage();

assertTrue(actualMessage.contains(expectedMessage));
}
}
}
Loading
Loading