A collection of methods arrived at through TDD to prepare for possible Java challenges raised in an interview.
- Create a class.
- Create a test for that class (Alt+Enter).
- Declare the class in the test class (Challenges challenges;).
- Create a test method.
- Initialize a new instance of the class before each method (challenges = new Challenges();).
- Create a failing test.
- Simulate the desired return of your class to make the test pass.
- Refactor your class to produce the desired return.
This is the process of how we write the tests themselves.
- Arrange - Create objects and test data: Challenges challenges = new Challenges();
- Act - Create the actions you want to test (call your method): int result = challanges.sum();
- Assert - Check that the right things have happened: assertThat(result, is(4));
- Red - Write a failing test (to check against a false positive).
- Green - Write minimal pass.
- Refactor - Implement client requirements.
Keystroke | Description |
---|---|
Alt+Enter | Create test from class, create method from test |
Alt+Insert | Generate Constructor, Getters / Setters, Test, etc. |
Alt+Shift+Click | Multiple Cursors |
Alt+J | Select next occurrence |
Alt+Shift+ J | Unselect this occurrence |
Ctrl+Shift+Alt+J | Select all occurrences |
Shift+F10 | Run |
Write a short program that prints each number from 1 to 100 on a new line.
For each multiple of 3, print "Fizz" instead of the number.
For each multiple of 5, print "Buzz" instead of the number.
For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.
for i = 1 to n
if i % 3 AND i % 5 == 0, "FizzBuzz"
else if i % 3 == 0, "Fizz"
else if i % 5 == 0, "Buzz"
else print i
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
list: [2, 3, 7, 4, 8]
result: 7
expected: [1, 3]
o(n^2) solution: outer / inner loop
for i = 0 to n
for j = 0 to n
arr[i] + arr[j] == result
Better solution:
numMap = empty Map
for i = 0 to n
delta = target - arr[i]
if numMap contains edlta
return [i, numMap(delta).value]
numMap.put (arr[i], i)