Frequently Asked Prescreen / Coding Questions

Quick Answers to a few Commonly Asked Coding Questions

I've been writing code for a few decades.
I wrote this page just to save us all some time. It is my hope that this page will allow us to skip most of the more obvious screening questions.
You really should also see my GitHub code samples.

Please feel free to ask any questions you like.

1. What is the difference between a Hashmap and a Hashtable? 1. HashMap is non synchronized and not thread safe.On the other hand, HashTable is thread safe and synchronized. Use HashMap in non-threading applications, it's slightly faster because it doesn't lock. The best form of locking HashTable is, ironically called, ConcurrentHashMap. It locks on a per bucket basis.
2. With a HashMap you can have one Null key and any number of Null values. Hashtables do not allow null keys and null values.
3. Hashmap object values are iterated by using iterator. HashTables use enumerator.
2. What is the difference between LinkedList and ArrayList?
Both implement the List interface. The underlying data structures, linked list vs array make up the difference.
Array searches are index based and thus are faster. Array provides O(1) performance for get(index). However altering the structure of an Array is costly. Insertions require moving memory around. Also, common implementations of Array use more memory than needed in order to avoid frequent reallocation, a tradeoff made for list alteration purposes.
Linked lists use only the memory needed for data plus pointer to the next element (doubly linked lists have back pointers). It is easy to modify a linked list, but searching them is less efficient. O(n) in worst case.
If you are going to modify data in a list frequently then a linked list is your best bet. If you are going to spend most of your time accessing data rather than changing it, then array is probably best. Which you use is application dependent.
3. What is the best way to concatenate String in Java? Some people think the compiler or JVM optimizes String into StringBuffer or StringBuilder when it sees String being concatinated. It does not. Building strings using StringBuffer or StringBuilder (almost exactly the same performance) is much faster than simply adding String together.
See this sample code. It shows the difference and allows you to do a time comparison. Feel free to copy it and try this yourself.
Results of the run:
4. Tell me how a Hashtable works. Can you write one? Sure. See
5. Can you write a piece of code to reverse a string. Sure. Here is a Java version that does not allocate memory for a second string. Same algorithm in C.
6. What are some of your favorite new Java 8 features? I'm a big fan of the new Collections Stream API, that sames a bit of time and code. Lamdas, which Java got from Python. A nice Concurrency API improvement is the Executor. I have about a year of exposure to Java 8.

If you ask any new ones I'll be sure to add the best ones to this list. :-)

Return to Job Search FAQ