17 Java coding tips for job interviews and pre-interview coding tests

Java coding questions are very common in job interviews. Good coding skills are essentials for passing the peer code reviews with flying colors. Here are 17 coding tips with Java examples.

Tip #1: If you are asked to write a function or code, make sure that your code fails fast. Check for pre-conditions and post conditions. For, example, if you are writing a function to “apply given interest rate to given balance”.

Pre-condition: Balance is a positive number and greater than zero. Interest rate is not a percentage and can’t be > 1.
Post-condition: new balance is a positive number by applying the interest rate.

Tip #2: Don’t write big functions, and divide them into smaller meaningful functions as shown above with isGreaterThanZero, isInputPercentage, etc.  Favor meaningful function names as opposed to cluttering your code with comments. The function names must be self explanatory as to what you are doing.

Tip #3: Write JUnit tests to test your function or code. Test for both the positive and negative scenarios. You need junit-xxx.jar for the example below.

Tip #4:If you are asked to write iterative version of function then you may be drilled  to write recursive version or vice-versa. When writing loops, don’t use i or j as index variables. Use meaningful names. Also avoid nested loops where possible.

Reverse a given string input : iteration

Reverse a given string input : recursion

Note: You may be quizzed on tail recursion. Recursion Vs Tail Recursion

Tip #5:  Familiarize yourself with order of complexity with the Big O notation to answer any follow up question like — What is the order of complexity of the above function you wrote?

Tip #6: Code to interface. Initialize your collection with appropriate capacity. Don’t return a null collection, instead return an empty collection like  Collections.emptyList( );.

Don’t do:


Map and List are interfaces.

Tip #7: Favor composition over interface

Tip #8: Use generics where required. Recursion with Generics know how. Understanding generics with scenarios.

Tip #9: Reduce the accessibility, and make your objects immutable where appropriate. For example, a custom key class that is used to store objects in a map. How to make a Java class immutable?.

Tip #10: Don’t forget to override Object class’s methods that are meant to be overridden like equals(…), hashCode( ), and toString( ).

Tip #11: When you have large if/else or switch statements, see if the Open/Closed design principle can be applied.

Tip #12: Don’t assume that your objects and classes are going to be accessed by a single-thread. Safeguard your objects and classes against multi-threading. Java multi-threading questions and answers.

Tip #13: Use the right data types. For example, use either BigDecimal to represent monetary values in dollars and cents or long to represent them in their lowest units as just cents (400 cents for $4.0). Beware of max and minimum values for data types. If you want values larger than long, use BigInteger, which has no theoretical limit. The BigInteger class allocates as much memory as it needs to hold all the bits of data it is asked to hold

byte (-128 to 127) –>  short (2 bytes,  -32,768 to 32,767) –> char(2 bytes, 65,535) –> int (4 bytes, -2^31 to 2^31-1) –> long(8 bytes,  -2^63 to 2^63-1) –> float (4 bytes) –> double (8 bytes). boolean (true or false)

Also, never compare floating point variables to compare exact amounts like

Tip #14: Handle and propagate exceptions properly. Have a consistent usage of checked and unchecked exceptions.

Exception is swept under the carpet.

CustomCheckedException is never reached as exceptions are polymorphic in nature. CustomCheckedException must be declared first above Exception.

Exception handling in Java questions and answers.

Tip #15: Write pseudo code for more difficult coding questions. At times drawing diagrams can help you solve the problem.

Tip #16: Keep in mind some of the design principles like  SOLID design principles, Don’t Repeat Yourself (DRY), and Keep It Simple ans Stupid (KISS). Also, think about the OO concepts — A PIE.   Abstraction, Polymorphism, Inheritance, and Encapsulation. These principles and concepts are all about accomplishing

  • Low coupling: Coupling in software development is defined as the degree to which a module, class, or other construct, is tied directly to others. You can reduce coupling by defining intermediate components (e.g. a factory class or an IoC container like Spring) and interfaces between two pieces of a system. Favoring composition over inheritance can also reduce coupling. 
  • High cohesion: Cohesion is the extent to which two or more parts of a system are related and how they work together to create something more valuable than the individual parts. You don’t want a single class to perform all the functions (or concerns) like being a domain object, data access object, validator, and a service class with business logic. To create a more cohesive system from the higher and lower level perspectives, you need to break out the various needs into separate classes.  

How will you go about writing loosely coupled Java applications?

Tip #17: If you are coding in front of the interviewer, think out loud. It is not always possible to solve problems in an interview when you are nervous, but how you will go about solving a coding problem is equally important even if you don’t solve it. So, let the interviewer know that you know how to approach a problem. Solve the problem first, and then find ways to optimize it in terms of performance and memory allocation.

Practice!, practice!!, practice!!!….Write  code!!, more code!!!, more code!!!!.  That is the only way. Any more tips or corrections folks?

300+ Java & Big Data Interview FAQs

16+ Java Key Areas Interview Q&As

800+ Java Interview Q&As

300+ Java & Big Data Tutorials