{"id":3252,"date":"2023-03-20T18:25:06","date_gmt":"2023-03-20T17:25:06","guid":{"rendered":"https:\/\/www.xenonique.co.uk\/blog\/?p=3252"},"modified":"2023-03-20T18:25:08","modified_gmt":"2023-03-20T17:25:08","slug":"java-interview-question-2-duplicate-integers-and-sort","status":"publish","type":"post","link":"https:\/\/www.xenonique.co.uk\/blog\/2023\/03\/20\/java-interview-question-2-duplicate-integers-and-sort\/","title":{"rendered":"Java Interview Question 2 &#8211; Duplicate Integers and Sort"},"content":{"rendered":"\n<p>Here is an online programming problem that you could encounter when interviewing for a contract  \/ permanent job. <\/p>\n\n\n\n<p>You have a Java developer environment with a unit test class <strong>DuplicateIntegersTest<\/strong> (JUnit 5) and you are using a Java 11 \/ Java 17. You have to write the logic that passes the following test. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage uk.co.xenonique.example;\n\nimport org.junit.jupiter.api.*;\n\nimport java.util.*;\nimport java.util.stream.*;\n\nimport static org.hamcrest.MatcherAssert.assertThat;\nimport static org.hamcrest.Matchers.is;\nimport static org.hamcrest.Matchers.notNullValue;\n\npublic class DuplicateIntegersTest {\n\n    @DisplayName(&quot;Given a set of integer When find duplicate integers Then return sorted set 1&quot;)\n    @Test\n    public void search_for_duplicates_1() {\n        var text = search_duplicates_sort_them(List.of(1, 2, 3, 4, 5, 6, 7, 7, 4));\n        assertThat(text, is(notNullValue()));\n        assertThat(text, is(&quot;4, 7&quot;));\n    }\n\n}\n\n<\/pre><\/div>\n\n\n<p>The exercise is about writing code function <strong>search_duplicates_sort_them()<\/strong>. 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&#8217;t care. The test is about writing the answer to solve the acceptance criteria. <\/p>\n\n\n\n<p>(You would be given up to 20 minutes to complete the function)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We could inline the function invocation and dispense with the local variable <strong>text<\/strong>, 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 <em>Mothering Sunday in the UK<\/em>)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>What do you write as your function version<strong> search_duplicates_sort_them()<\/strong>? You need to impress me the technical lead of this project.<\/p>\n\n\n\n<p>Here are some more test conditions. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npublic class DuplicateIntegersTest {\n\n    \/\/ ...\n\n    @DisplayName(&quot;Given a set of integer When find duplicate integers Then return sorted set 2&quot;)\n    @Test\n    public void search_for_duplicates_2() {\n        var text = search_duplicates_sort_them(List.of(3,3,3,3,3,3));\n        assertThat(text, is(notNullValue()));\n        assertThat(text, is(&quot;3&quot;));\n    }\n\n    @DisplayName(&quot;Given a set of integer When find duplicate integers Then return sorted set 3&quot;)\n    @Test\n    public void search_for_duplicates_3() {\n        var text = search_duplicates_sort_them(List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 3, 2, 1));\n        assertThat(text, is(notNullValue()));\n        assertThat(text, is(&quot;1, 2, 3&quot;));\n    }\n\n    @DisplayName(&quot;Given a set of integer When find duplicate integers Then return sorted set 4&quot;)\n    @Test\n    public void search_for_duplicates_4() {\n        var text = search_duplicates_sort_them(List.of(9, 8, 7, 6, 5, 4, 3, 2, 1, 6, 4, 2));\n        assertThat(text, is(notNullValue()));\n        assertThat(text, is(&quot;2, 4, 6&quot;));\n    }\n\n    @DisplayName(&quot;Given a set of integer When find duplicate integers Then return sorted set 5&quot;)\n    @Test\n    public void search_for_duplicates_5() {\n        var text = search_duplicates_sort_them(List.of());\n        assertThat(text, is(notNullValue()));\n        assertThat(text, is(&quot;&quot;));\n    }\n\n}\n<\/pre><\/div>\n\n\n<p><\/p>\n\n\n\n<p>I will give you the answers in a few days.<\/p>\n\n\n\n<p>Now you code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npublic String search_duplicates_sort_them(List&lt;Integer&gt; yourNumbers) {\n    \/\/ What?\n}\n<\/pre><\/div>\n\n\n<p>Peter Pilgrim<\/p>\n\n\n\n<p>Monday 20th March 2023<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. The exercise [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[75,4,27,65,214,52,17],"tags":[],"_links":{"self":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/3252"}],"collection":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=3252"}],"version-history":[{"count":2,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/3252\/revisions"}],"predecessor-version":[{"id":3254,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/posts\/3252\/revisions\/3254"}],"wp:attachment":[{"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=3252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=3252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xenonique.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=3252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}