When an interview question is asked in English, will it be diffcult to answer? Found someting about how to answer top java interview questions.
The Java Job Interview
The best way to prepare for your interview starts with having a realistic resume. If you put on your resume that you’ve used JDBC or Security, you better be prepared for someone to ask you a question about those technologies.
The more specific a technology, like Hibernate, the higher the likelihood of getting asked a question about it, if it is a technology the company is interested in your specific experience. Don’t put technologies on your resume that you think might help with automated keyword searches, but your exposure to the technologies is so sparse or non-existent that you can’t answer a basic technical question about the technology.
You will be expected to know computer science fundamentals. How much of that depends on your level. These computer science questions may actually be easier for the more beginning developer, as someone straight out of college might have just had a course on algorithms perhaps. Think data structures and performance-related here.
The Interview Coding Test — How to Work the White Board
The more experienced you are, the more you will be expected to know how to code AND how to read code. Expect to have to go to a white board and solve a problem. Get comfortable doing it.
When coding the solution to a problem on a white board in a job interview, talk through your solution. It may seem obvious, but don’t just code the whole solution silently and ask if the questioner has any questions when you are done.
Don’t just code a brute force solution. More than likely, that isn’t what the questioner is looking for. Also, once completed, the interviewer could just move on to the next question. If not, you’ll be taking up valuable time correcting to the right solution after, and more importantly, you might do it wrong.
When responding to coding problems, there are a few guidelines to follow. Some may seem obvious, but all are worth reminding.
- Listen carefully to the questions. If you’re unsure of a question, ask for clarification, don’t just answer what you think the interviewer might be looking for.
- Don’t make assumptions. If asked to code something like a binary search, don’t just assume an array of numbers.
- Listen for hints. If your interviewer gives you a hint, don’t miss it, but more importantly don’t ignore it. They’re trying to push you in the right direction to an answer.
Questions About Computer Science Fundamentals
The most common set of questions you’ll face test your knowledge of computer science. Why do you pick one data structure over another? How do you code a particular algorithm?
It might seem simple, but could you code binary search in the Java programming language?
Here is the implementation of the algorithm from the java.util.Arrays class for an int array source:
private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) { int low = fromIndex; int high = toIndex - 1; while (low <= high) { int mid = (low + high) >>> 1; int midVal = a[mid]; if (midVal < key) low = mid + 1; else if (midVal > key) high = mid - 1; else return mid; // key found } return -(low + 1); // key not found. }
First, could you code this off the top of your head? Second, do any lines look different than your knowledge of the algorithm? What does the “>>> 1” do? That’s basically a divide by two operation.
I would say doing “/ 2” would suffice there. The return value of “-(low +1)” gives the caller the insert position where to insert the new key, if they wanted to do that task.
Ask your interviewer, -1 might suffice to say something isn’t there. The “-(low + 1)” bit could be a great follow-on question from the interviewer after you get the coded algorithm properly.
Don’t assume after you have a successful solution you are done with that problem.
Introductory Java Interview Questions
As your skill level increases, the questions you receive get more complicated. That doesn’t excuse you from knowing the introductory questions, too, if you are more experienced.
The obvious set of questions you’ll be asked test your knowledge of the Java API. The range of questions you will be asked here is enormous. Fresh out of school, you might be asked about the different keywords in Java. Been around the block a few times, then write a regular expression that describes the format of an email address.
I was once asked the latter question and I didn’t even mention regular expressions on my resume.
Here are a couple sample questions and answers of what to expect at this level.
Sample Introductory Java Question 1
Describe what all the different parts of the main() method declaration mean.
How to Answer
The first thing you should do here is write the main() method declaration on the whiteboard:
public static void main(String[] args)
Next, describe what each of public, static, void, main, String[], and args does.
It is probably sufficient to just verbalize what each means. But if you want to write each word down as you verbalize, it helps to get the point across.
a) public means that the method is visible everywhere
b) static means you don’t need an instance of the class to call the method
c) void means the method doesn’t return anything
d) main is just the predefined method name for how to start a Java program
e) String[] means the method accepts an array of strings as arguments to the method
f) args is the name of the parameter to the method. Any name will do here. There is no hard and fast requirement that the parameter be called args.
Sample Introductory Java Question 2
Given a line of text, verify that the beginning and ending parenthesis match up.
How to Answer
This is a little more complicated than the first, but still a coding problem a junior person should be able to code.
The easiest way to do this is to push each beginning paran onto a stack, then when you get to a closing paran, pop an item off the stack. If the parens match (‘(‘ and ‘)’ or ‘{‘ and ‘}’) continue.
If not, there is a mismatch. If the pop is ever empty or the stack isn’t empty at the end, there is a mismatch. Here is one solution that relies on the java.util.Stack class.
import java.util.Stack; public class Match { public static boolean doParensMatch(String str) { Stack stack = new Stack<>(); char c; for (int i=0; i < str.length(); i++) { c = str.charAt(i); if (c == '(' || c == '{') { stack.push(c); } else if (c == '}') { if (stack.empty()) { return false; } else if (stack.peek() == '{') { stack.pop(); } } else if (c == ')') { if (stack.empty()) { return false; } else if (stack.peek() == '(') { stack.pop(); } else { return false; } } } return stack.empty(); } public static void main(String[] args) { System.out.println(doParensMatch("")); System.out.println(doParensMatch("()")); System.out.println(doParensMatch("{}")); System.out.println(doParensMatch("{()}")); System.out.println(doParensMatch("{123}")); System.out.println(doParensMatch("{)")); // failure System.out.println(doParensMatch("{((((((((()")); // failure } }
Three things worth pointing out here:
1. Recursion isn’t the answer to everything.
2. The main() method declaration here demonstrates lots of test cases. Don’t just code a solution. Walk through the solution with at least one if not more test cases.
3. The Stack<Character> stack = new Stack<>(); line requires special mention.
First, the <Character> bit demonstrates at least some knowledge of generics. Secondly, the <> declaration shows you’ve kept up with the language updates and know you don’t have to do <Character> again. Little things like that are things that interviewers are looking for.
You won’t automatically fail with a line like Stack stack = new Stack() and casting when you get a character off the stack, but the more up to date you are with your code, the better you’ll look.
Advanced Java Interview Questions
As you get more experienced, you can expect more advanced types of coding problems. Not only will you be expected to code the solution, but you’ll be expected to explain your answer more eloquently.
Even more important with advanced Java programming questions is talking your way through the possible solution before coding.
Returning to that email regex expression mentioned earlier, here’s a simple solution that isn’t fully RFC822 compliant but would probably suffice for the purposes of an interview.
Pattern regexPattern = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE); Matcher matcher = regexPattern.matcher(emailStr); boolean found = matcher.find();
Can you come up with something comparable? If given that solution, could you explain what the different elements of the pattern do? What do the ^, $, +, and {2,6} indicate? (start of line/string, end of line/string, at least once, and at least 2 but not more than 6 times, respectively).
Given the recent addition of new top level domains, that 6 is now too short. Good thing to point out if given that code as a possible solution.
The CASE_INSENSITIVE isn’t an absolute requirement, but does simplify the solution a little. Also, if asked to write the pattern, the compile(), matcher(), and find() calls aren’t required either, but help to demonstrate your knowledge of the API to actually use the regular expression.
On the more advanced side, you should expect pattern-related questions. These would range from describe the Singleton design pattern and how you would use it, to where in Java is the Singleton design pattern used, to code the Singleton design pattern, and finally what issues serialization can bring up with the Singleton design pattern. Observer, Factory, Decorator patterns are some others to be aware of that are found in Java.
The best way to prepare for the more advanced interview questions for a particular company is to actually use Google to lookup what other people are reporting they were asked. This works great for larger companies.
For instance, with a company like Google itself, you might run across a resource they provide: https://sites.google.com/site/coachingsessions1/interview-resources.
When asked more advanced questions, the solution doesn’t stop when the question is answered. You need to expect follow-up questions, like why did you do the operation in a particular way, what is the performance of your solution, and how can you improve it.
For some reason, many of the more advanced questions are game related. Don’t be surprised if you have to code the implementation of some game, or a part thereof.
Some questions I’ve been asked include finding words on a Boggle board, reporting which if any player won from a Tic-Tac-Toe board, and dealing several card game related questions.