Interviews often have Code Challenges - getting good at Algorithms helps
Companies nowadays often filter applicants using Online Code Challenges such as those found at:
Hackerranks even has online IDEs which behind the scenes are integrated with docker images being booted up in to support the tests. Hackerranks write some tests and the tests can even be run against a Spring Boot Application that you are coding inside the online IDE. The Online IDEs are now pretty impressive and boast intellisense code completion.
There are also a relatively new bunch of American companies that boast they find the top scoring 1% of devs from all over the world and find them remote jobs working for US companies:
There are also some companies starting to do the same thing in Europe:
These companies ALL require you to do online code challenges.
To support these online code challenges there are a bunch of websites where you can practise code challenges:
There are even websites that have sprung up which attempt to explain solutions to problems on leetcode.com:
For example neetcode.io has chosen what it thinks are the 75 best questions to practise with:
https://neetcode.io/practice ==> Choose the blind75 tab
Note that blind75 questions also have some video explanations of how to solve the problem.
JD has been implementing some of the blind75 code challenges and you can retrieve them from:
git clone https://github.com/spotadev/java_algorithm_tests.git
JD added Unit tests and some interfaces.
For example under package:
com.spotadev.algo.blind75.arraysandhashing.easy.contains_duplicates
JD added:
src/main/java/ContainsDuplicatesAPI
src/test/java/AbstractContainsDuplicatesTest
To code your solution you need to implement the ContainsDuplicatesAPI. e.g.
ContainsDuplicates_JD
and you need to extend AbstractContainsDuplicatesTest e.g.
ContainsDuplicates_JDTest
So you can do stuff like copy ContainsDuplicates_JD and ContainsDuplicates_JDTest and rename it with your initials. i.e. instead of JD put your initials.
JD spent some time creating these Abstract test cases. Note that on places like leetcode.com you type your solution in a box and it is run across leetcode's unit tests.
If your test fails you can add some more test cases to the Abstract Test Cases if you want. JD already added quite a lot of tests.
package com.spotadev.algo.blind75.arraysandhashing.easy.contains_duplicates;
/**
* @author John Dickerson - 13 Jun 2022
*/
public interface ContainsDuplicatesAPI {
boolean containsDuplicate( int[] nums );
}
package com.spotadev.algo.blind75.arraysandhashing.easy.contains_duplicates;
import org.testng.Assert;
/**
* @author John Dickerson - 13 Jun 2022
*/
public abstract class AbstractContainsDuplicatesTest {
protected ContainsDuplicatesAPI containsDuplicates;
public void containsDuplicateTest_1() {
boolean hasDuplicates =
containsDuplicates.containsDuplicate( new int[] { 1, 2, 3, 1 } );
Assert.assertTrue( hasDuplicates );
}
public void containsDuplicateTest_2() {
boolean hasDuplicates =
containsDuplicates.containsDuplicate( new int[] { 1, 2, 3, 4 } );
Assert.assertFalse( hasDuplicates );
}
public void containsDuplicateTest_3() {
boolean hasDuplicates =
containsDuplicates.containsDuplicate( new int[] { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 } );
Assert.assertTrue( hasDuplicates );
}
}
package com.spotadev.algo.blind75.arraysandhashing.easy.contains_duplicates;
import java.util.HashSet;
import java.util.Set;
/**
* https://neetcode.io/
*
* https://www.youtube.com/watch?v=3OamzN90kPg
*
* https://leetcode.com/problems/contains-duplicate/
*
* Runtime: 6 ms, faster than 94.83% of Java online submissions for Contains Duplicate.
*
* Memory Usage: 54.7 MB, less than 88.35% of Java online submissions for Contains Duplicate.
*
* Given an integer array nums, return true if any value appears at least twice in the array,
* and return false if every element is distinct.
*
* Example 1:
*
* Input: nums = [1,2,3,1]
* Output: true
*
* Example 2:
*
* Input: nums = [1,2,3,4]
* Output: false
*
* Example 3:
*
* Input: nums = [1,1,1,3,3,4,3,2,4,2]
* Output: true
*
*
* Constraints:
*
* 1 <= nums.length <= 10^5
* -109 <= nums[i] <= 10^9
*
* @author John Dickerson - 30 Apr 2022
*/
public class ContainsDuplicates_JD implements ContainsDuplicatesAPI {
/**
* Time:
*
* O(n)
*
* In worse case we could cycle through all of n
*
* Memory:
*
* O(n)
*
* Sets use Arrays and the Array are pre-initialized with:
*
* 8 byte header
* 4 byte size
* Bytes for the type of each slot
* Total bytes has to be multiple of 8 so padding bytes added if necessary
*/
@Override
public boolean containsDuplicate( int[] nums ) {
Set<Integer> numbers = new HashSet<>();
for ( int num : nums ) {
if ( numbers.contains( num ) ) {
return true;
}
numbers.add( num );
}
return false;
}
}
package com.spotadev.algo.blind75.arraysandhashing.easy.contains_duplicates;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
/**
* @author John Dickerson - 30 Apr 2022
*/
public class ContainsDuplicates_JDTest extends AbstractContainsDuplicatesTest {
@BeforeClass
public void setUp() {
containsDuplicates = new ContainsDuplicates_JD();
}
@Test
public void containsDuplicateTest_1() {
super.containsDuplicateTest_1();
}
@Test
public void containsDuplicateTest_2() {
super.containsDuplicateTest_2();
}
@Test
public void containsDuplicateTest_3() {
super.containsDuplicateTest_3();
}
}
Rather than adding all the examples here we have decided to just let people clone the examples from here:
git clone https://github.com/spotadev/java_algorithm_tests.git
The reason is the solutions are in a continuous state of flux and we do not want the text written on this blog to get out of date with the code.
Back: Java
Page Author: JD