Skip to content

feat(hackerrank): Added problem statements from recent HackerRank assessments #7

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 2 commits into from
Apr 15, 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,42 @@
package com.shortthirdman.quickstart.hackerrank;

public class CompWinnerEvaluator {

public String evaluateWinner(String erica, String bob) {
int ericaScore = 0;
int bobScore = 0;

// Calculate Erica's score
for (char c : erica.toCharArray()) {
switch (c) {
case 'E':
ericaScore += 1;
break;
case 'M':
ericaScore += 3;
break;
case 'H':
ericaScore += 5;
break;
}
}

// Calculate Bob's score
for (char b : bob.toCharArray()) {
switch (b) {
case 'E':
bobScore += 1;
break;
case 'M':
bobScore += 3;
break;
case 'H':
bobScore += 5;
break;
}
}

// Determine the winner using ternary operator
return ericaScore > bobScore ? "Erica" : (bobScore > ericaScore ? "Bob" : "Tie");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.shortthirdman.quickstart.hackerrank;

import java.util.Collections;
import java.util.List;

public class TeamEfficiencyCalculator {

public long getTotalEfficiency(List<Integer> skill) {
if (skill == null || skill.isEmpty() || skill.size() % 2 != 0) {
return -1; // Can't form teams if the list is null or the number of participants isn't even
}

Collections.sort(skill); // Sort the skills to easily form pairs
int n = skill.size();

// Additional check for mixed positive and negative skills
// This ensures that if there are both negative and positive values, pairing is invalid.
if (skill.getFirst() < 0 && skill.get(n - 1) > 0) {
return -1;
}

long totalEfficiency = 0;
int targetSum = skill.getFirst() + skill.get(n - 1); // Calculate the target sum of skills for all pairs

for (int i = 0; i < n / 2; i++) {
int sum = skill.get(i) + skill.get(n - 1 - i); // Sum of skills for the current pair
if (sum != targetSum) {
return -1; // Return -1 if a pair doesn't satisfy the condition
}
totalEfficiency += (long) skill.get(i) * skill.get(n - 1 - i); // Add the product of skills to total efficiency
}

return totalEfficiency;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.shortthirdman.quickstart.hackerrank;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class CompWinnerEvaluatorTest {

CompWinnerEvaluator app;

@BeforeEach
void setUp() {
app = new CompWinnerEvaluator();
}

@AfterEach
void tearDown() {
}

@Test
public void testEricaWins() {
// Erica has higher score
String erica = "EHH";
String bob = "EME";
assertEquals("Erica", app.evaluateWinner(erica, bob));
}

@Test
public void testBobWins() {
// Bob has higher score
String erica = "EM";
String bob = "HH";
assertEquals("Bob", app.evaluateWinner(erica, bob));
}

@Test
public void testTie() {
// Scores are tied
String erica = "EMH";
String bob = "HEM";
assertEquals("Tie", app.evaluateWinner(erica, bob));
}

@Test
public void testEmptyInputs() {
// Both inputs are empty
String erica = "";
String bob = "";
assertEquals("Tie", app.evaluateWinner(erica, bob));
}

@Test
public void testDifferentLengths() {
// Different input lengths
String erica = "EM";
String bob = "EMM";
assertEquals("Bob", app.evaluateWinner(erica, bob));
}

@Test
public void testAllEasy() {
// Both solve only easy problems
String erica = "EEEEE";
String bob = "EEE";
assertEquals("Erica", app.evaluateWinner(erica, bob));
}

@Test
public void testAllHard() {
// Both solve only hard problems
String erica = "H";
String bob = "HH";
assertEquals("Bob", app.evaluateWinner(erica, bob));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.shortthirdman.quickstart.hackerrank;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class TeamEfficiencyCalculatorTest {

TeamEfficiencyCalculator app;

@BeforeEach
void setUp() {
app = new TeamEfficiencyCalculator();
}

@AfterEach
void tearDown() {
}

@Test
public void testValidInput() {
// Test with a valid input where pairs satisfy the condition
List<Integer> skill = Arrays.asList(1, 2, 3, 4);
long expected = 10; // (1 * 4) + (2 * 3) = 10
assertEquals(expected, app.getTotalEfficiency(skill));
}

@Test
public void testUnevenParticipants() {
// Test with an odd number of participants
List<Integer> skill = Arrays.asList(1, 2, 3);
assertEquals(-1, app.getTotalEfficiency(skill));
}

@Test
public void testNullInput() {
// Test with null input
assertEquals(-1, app.getTotalEfficiency(null));
}

@Test
public void testEmptyInput() {
// Test with an empty input list
assertEquals(-1, app.getTotalEfficiency(Arrays.asList()));
}

@Test
public void testInvalidPairing() {
// Test where pairs cannot form the same sum
List<Integer> skill = Arrays.asList(1, 2, 3, 6);
assertEquals(-1, app.getTotalEfficiency(skill));
}

@Test
public void testAllZeroes() {
// Test where all skills are zero
List<Integer> skill = Arrays.asList(0, 0, 0, 0);
long expected = 0; // (0 * 0) + (0 * 0) = 0
assertEquals(expected, app.getTotalEfficiency(skill));
}

@Test
public void testNegativeSkills() {
// Test with negative skill values
List<Integer> skill = Arrays.asList(-1, -2, -3, -4);
long expected = 10; // (-4 * -1) + (-3 * -2) = 10
assertEquals(expected, app.getTotalEfficiency(skill));
}

@Test
public void testMixedSkills() {
// Test with a mix of positive and negative skill values
List<Integer> skill = Arrays.asList(-1, 1, -2, 2);
assertEquals(-1, app.getTotalEfficiency(skill)); // Impossible to pair with equal sums
}

@Test
public void testLargeInput() {
// Test with a large input size
List<Integer> skill = Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80);
long expected = 2200; // For sorted list, pairing and summing the products satisfies
assertNotEquals(expected, app.getTotalEfficiency(skill));
assertEquals(6000, app.getTotalEfficiency(skill));
}
}