/Java-Challanges

A collection of methods arrived at through TDD to prepare for possible Java challenges raised in an interview.

Primary LanguageJava

Java Interview Challenges

A collection of methods arrived at through TDD to prepare for possible Java challenges raised in an interview.

TDD Procedure:

  1. Create a class.
  2. Create a test for that class (Alt+Enter).
  3. Declare the class in the test class (Challenges challenges;).
  4. Create a test method.
  5. Initialize a new instance of the class before each method (challenges = new Challenges();).
  6. Create a failing test.
  7. Simulate the desired return of your class to make the test pass.
  8. Refactor your class to produce the desired return.

Arrange, Act, Assert

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, Green, Refactor

  • Red - Write a failing test (to check against a false positive).
  • Green - Write minimal pass.
  • Refactor - Implement client requirements.

Shortcuts

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

Challenges

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.

Pseudo Code

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.

Pseudo Code

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)