PEAT Online Course -
How To Improve Your Personal Performance in Java work Technology

  • YOU + CONTROL
  • Focus
  • Autonomy
  • Proficiency
  • Purpose
  • Covid-19!!!
  • Deep Hidden Meaning
  • FREE updates
GET 75% OFF NOW
Your Promo code: TECHLAYOFF
Exclusively on
VIMEO ON-DEMAND

Java Interview Question 2 – Duplicate Integers and Sort

20 March 2023 1 comment

3 minutes

453

Here is an online programming problem that you could encounter when interviewing for a contract / permanent job.

You have a Java developer environment with a unit test class DuplicateIntegersTest (JUnit 5) and you are using a Java 11 / Java 17. You have to write the logic that passes the following test.

package uk.co.xenonique.example;

import org.junit.jupiter.api.*;

import java.util.*;
import java.util.stream.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

public class DuplicateIntegersTest {

    @DisplayName("Given a set of integer When find duplicate integers Then return sorted set 1")
    @Test
    public void search_for_duplicates_1() {
        var text = search_duplicates_sort_them(List.of(1, 2, 3, 4, 5, 6, 7, 7, 4));
        assertThat(text, is(notNullValue()));
        assertThat(text, is("4, 7"));
    }

}

The exercise is about writing code function search_duplicates_sort_them(). I deliberately wrote the function name is lower to throw off the candidate, by the way. You can write in camel case or not. I don’t care. The test is about writing the answer to solve the acceptance criteria.

(You would be given up to 20 minutes to complete the function)

We could inline the function invocation and dispense with the local variable text, but how would we check for null reference in the test? In Kotlin you do not need to concern yourself about null pointers, because this newer JVM language has improved type safety features, but this code is about the Mother language Java. (I think this is appropriate since yesterday 19th May 2023 was Mothering Sunday in the UK)

What do you write as your function version search_duplicates_sort_them()? You need to impress me the technical lead of this project.

Here are some more test conditions.

public class DuplicateIntegersTest {

    // ...

    @DisplayName("Given a set of integer When find duplicate integers Then return sorted set 2")
    @Test
    public void search_for_duplicates_2() {
        var text = search_duplicates_sort_them(List.of(3,3,3,3,3,3));
        assertThat(text, is(notNullValue()));
        assertThat(text, is("3"));
    }

    @DisplayName("Given a set of integer When find duplicate integers Then return sorted set 3")
    @Test
    public void search_for_duplicates_3() {
        var text = search_duplicates_sort_them(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 2, 1));
        assertThat(text, is(notNullValue()));
        assertThat(text, is("1, 2, 3"));
    }

    @DisplayName("Given a set of integer When find duplicate integers Then return sorted set 4")
    @Test
    public void search_for_duplicates_4() {
        var text = search_duplicates_sort_them(List.of(9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 4, 2));
        assertThat(text, is(notNullValue()));
        assertThat(text, is("2, 4, 6"));
    }

    @DisplayName("Given a set of integer When find duplicate integers Then return sorted set 5")
    @Test
    public void search_for_duplicates_5() {
        var text = search_duplicates_sort_them(List.of());
        assertThat(text, is(notNullValue()));
        assertThat(text, is(""));
    }

}

I will give you the answers in a few days.

Now you code:

public String search_duplicates_sort_them(List<Integer> yourNumbers) {
    // What?
}

Peter Pilgrim

Monday 20th March 2023

Hey all! Thanks for visiting. I provide fringe benefits to interested readers: checkout consultancy, training or mentorship Please make enquiries by email or
call +44 (0)7397 067 658.

Due to the Off-Payroll Working plan for the UK government, I am enforcing stricter measures on contracts. All potential public sector GOV.UK contracts engagements must be approved by QDOS and/or SJD Accounting. Please enquire for further information.