Rich got the job at Apple:
Thanks to your site, I have offers from Apple, Facebook and Google. That's a hell of an ROI! I didn't go to school for computer science, and now I have multiple senior engineering offers from the world's best tech companies.
Patrick got the job at Dropbox:
Honestly, this feels like cheating. Very efficient way to spend your study time and definitely pays for itself!
Cyrus got the job at Facebook:
Your site was an invaluable resource and really helped me develop the iterative algorithmic problem solving skills big tech companies & startups are looking for. Great case of quality over quantity.
Chris got the job at Palantir:
I used a number of resources to help prep for the coding interviews but Interview Cake stood out as by far and away the most useful. I owe you a massive debt of thanks
Eric got the job at Amazon:
Your site was really indispensable for the Google/Amazon interview process. I've recommended your site to everyone I know that wants to become both a better programmer and a better interviewer. Great work.
Zak got the job at Mixpanel:
I got offers from 7/8 of the companies at which I interviewed. After not going through a formal interview process in nearly a decade, your site really helped build my confidence. You're a hero, Parker ;)
Michael got the job at Google:
Your website was so helpful that I didn't just receive an offer from Google, I also received a competing offer from Amazon.
Luna got the job at Apple:
I wish I had known about this website back when I was in algorithms class in college. This made me finally understand a number of concepts that had eluded me for years.
Richie got the job at Microsoft:
Right now I have an offer from Microsoft and I'm still in the interview process with Google and LinkedIn. Life is pretty good, and I owe so much of that to Interview Cake.
Akshay got the job at Amazon:
Having tried several resources for interview prep, I can honestly say that Interview Cake provides the most structured and confidence-inducing approach.
Explain what kinds of problems singletons solve. Then implement a singleton class and test that is in fact a singleton. keep reading »
What's the Difference Between a String Literal and a String Object?How do I know which one to use in a given context? And should I ever use a naked char array instead? keep reading »
What's the Difference Between an int and a long in Java?How should I decide which one to use? keep reading »
What's the Difference Between an ArrayList, a LinkedList, and a Vector?How do I pick which one to use? keep reading »
Singletons are classes which can have no more than one object. They're most useful for storing global state across a system.
Some situations where one might use a singleton include:
Singletons are contentious these days. Many people believe they should be avoided, or at least be used less often than they generally are.
Even so, implementing a singleton is an interesting coding challenge.
Suppose we wanted a singleton called InstallationDetails that stored some information, including the licenseNumber. How would we implement this?
We have several options. The first is lazy: have the class get or create its instance just in time, as it's requested:
To make this thread-safe:
Another is to eagerly have the class instantiate its singleton object even before one is requested:
There's also the initialization-on-demand way:
This method is lazy like the first approach, and also thread-safe.
Then there's the enum way:
Regardless of which method we use, we can test that our class is indeed a singleton like so:
Pro Tip: Although learning the answers to these common Java trivia questions is important, it's so much more important to be able to quickly solve Java problems you've never seen before. Interviewers want to see that you can do more than just memorize facts!
If you really want to take your prep to the next level, and learn the right way of thinking to quickly solve new problems, you should check out my 7-day email course:
Cool, watch your inbox!
To hard-code a string in Java, we have two options. A string literal:
And a string object:
What's different about these two options?
When you use a string literal, the string is interned. That means it's stored in the "string pool" or "string intern pool". In the string pool, each string is stored no more than once. So if you have two separate variables holding the same string literals in a Java program:
Those two Strings don't just contain the same objects in the same order, they are in fact both pointers to the same single canonical string in the string pool. This means they will pass an '==' check.
If our Strings were instantiated as objects, however, they would not be "interned," so they would remain separate objects (stored outside of the string pool).
In some languages, like Lisp and Ruby, interned strings are called "symbols."
Can you intern a string "by hand?" Absolutely:
Given this, String literals can be thought of as syntactic sugar for instantiating a String and immediately interning it.
So which should you use, string literals or String objects?
You should almost always use String literals. Here's why:
It saves time. Comparing equality of interned strings is a constant-time operation, whereas comparing with .equals() is O(n) time.
It saves space. You can have several variables referencing one string while only storing that set of characters in one canonical place (the string pool).
Use String objects only if you want to be able to have two separate string objects with the same contents.
Bonus: consider storing sensitive strings (like passwords) as char arrays. This has a few nice features:
The Swing library, for example, has a getPassword method which always returns a char[].
32 bits. The difference is 32 bits :)
ints are 32-bit numbers, while longs are 64-bit numbers. This means ints take up half as much space in memory as longs, but it also means ints can't store as big of numbers as longs.
Here's a full listing of non-decimal number primitive types in Java, along with the maximum and minimum values they can hold:
name | bits | min value | max value |
---|---|---|---|
byte | 8 | -128 | 127 |
short | 16 | -32,768 | 32,767 |
int | 32 | -2^{31} (~-2 billion) | 2^{31}-1 (~2 billion) |
long | 64 | -2^{63} (~-9 "billion billion billion") | 2^{63}-1 (~9 "billion billion billion") |
Which should you use? It depends how big you expect your numbers to be. In general, you should use the data type that's big enough to hold the numbers you expect to store, but no bigger.
If you choose a type that's too small, you risk integer overflow. In Java, integers "silently" overflow—that is, no error is thrown, the integer simply goes from a very large value to a very small value. This can cause some very difficult-to-diagnose bugs. In Java 8, you can use Math.addExact and Math.subtractExact to force an exception to be thrown if the operation causes an overflow.
What's an example of a time when 32 bits is not enough? When you're counting views on a viral video. YouTube famously ran into trouble when the Gangnam Style video hit over 2^{31}-1 views, forcing them to upgrade their view counts from 32-bit to 64-bit numbers.
Given the threat of integer overflow, one might be tempted to just always use longs, "to be safe." But you'd risk using up more space than you needed to. Specifically, if you use longs when you could be using ints, you'll use twice as much space as you need to. If you're dealing with a big array of numbers that takes up several gigabytes of space, a space savings of one half is huge.
Another nice side effect of using the correctly-sized data type to store your numbers is that it serves a bit of documentation in your code—a reminder to yourself and to other engineers about the specific range of numbers you're expecting for a given variable.
Psssst.. Learn how to avoid the dreaded "whiteboard freeze". In our 7 day email crash course, I'll give you a strategy for quickly breaking down and solving any coding interview question.
Cool, watch your inbox!
The first difference is that LinkedList is, predictably enough, an implementation of a linked list. ArrayList and Vector, on the other hand, are implementations of dynamic arrays.
So LinkedList has the strengths and weaknesses of a linked list, while ArrayList and Vector have the strengths and weaknesses of a dynamic array. In particular:
So which should you use? Conventional wisdom is that dynamic arrays are usually the right choice. The main exception is if you plan to use Iterator.remove and/or ListIterator.add very heavily and you don't plan to use get very often. Then a LinkedList might be the right choice, although it may take up more memory and—because it's less cache-friendly—it may have slower reads.
So within our options for dynamic array data structures, which one should we choose?
The main difference (though there are others) is that Vector is entirely thread-safe, because it synchronizes on each individual operation.
But you should almost always use ArrayList, even if you're writing code that needs to be thread-safe. The reason is that synchronizing on each operation is generally not the best way to make your dynamic array thread-safe. Often what you really want is to synchronize a whole set of operations, such as looping through the dynamic array, making some modifications as you go. If you're going to be doing multiple operations while looping through a Vector, you'll need to take out a lock for that whole series of operations anyway, or else another thread could modify the Vector underneath you, causing a ConcurrentModificationException.
For this reason, Vectors are generally considered to be obsolete. Use an ArrayList instead, and manage any necessary synchronization by hand.
If you're ready to start applying these concepts to some problems, check out our mock coding interview questions.
They mimic a real interview by offering hints when you're stuck or you're missing an optimization.
I'm making a new search engine called MillionGazillion(tm), and I need help figuring out what data structures to use. keep reading »
You've implemented a Stack class, but you want to access the largest element in your stack from time to time. Write an augmented LargestStack class. keep reading »
Write a function to see if a binary tree is 'superbalanced'--a new tree property we just made up. keep reading »
Write a function to check that a binary tree is a valid binary search tree. keep reading »
Find the second largest element in a binary search tree. keep reading »
Implement a queue with two stacks. Assume you already have a stack implementation. keep reading »
Write a function that will replace your role as a cashier and make everyone rich or something. keep reading »
You've hit the mother lode: the cake vault of the Queen of England. Figure out how much of each cake to carry out to maximize profit. keep reading »
Figure out which number is repeated. But here's the catch: optimize for space. keep reading »
Figure out which number is repeated. But here's the catch: do it in linear time and constant space! keep reading »
For each number in an array, find the product of all the other numbers. You can do it faster than you'd think! keep reading »
Find the highest possible product that you can get by multiplying any 3 numbers from an input array. keep reading »
Write a function for merging meeting times given everyone's schedules. It's an enterprise end-to-end scheduling solution, dog. keep reading »
You're building a word cloud. Write a function to figure out how many times each word appears so we know how big to make each word in the cloud. keep reading »
Given an array of numbers in sorted order, how quickly could we check if a given number is present in the array? keep reading »
I wanted to learn some big words to make people think I'm smart, but I messed up. Write a function to help untangle the mess I made. keep reading »
Writing a simple recommendation algorithm that helps people choose which movies to watch during flights keep reading »
In a beautiful Amazon utopia where breakfast is delivered by drones, one drone has gone missing. Write a function to figure out which one is missing. keep reading »
Write a function to delete a node from a linked list. Turns out you can do it in constant time! keep reading »
Check to see if a linked list has a cycle. We'll start with a simple solution and move on to some pretty tricky ones. keep reading »
Write a function to reverse a linked list in place. keep reading »
Find the kth to last node in a singly-linked list. We'll start with a simple solution and move on to some clever tricks. keep reading »
Write a function to reverse a string in place. keep reading »
Write a function to reverse the word order of a string, in place. It's to decipher a supersecret message and head off a heist. keep reading »
Write a function that finds the corresponding closing parenthesis given the position of an opening parenthesis in a string. keep reading »
Write a super-simple JavaScript parser that can find bugs in your intern's code. keep reading »
Check if any permutation of an input string is a palindrome. keep reading »
Write a recursive function of generating all permutations of an input string. keep reading »
Efficiently sort numbers in an array, where each number is below a certain maximum. keep reading »
Computer the nth Fibonacci number. Careful--the recursion can quickly spin out of control! keep reading »
Find the repeat number in an array of numbers. Optimize for runtime. keep reading »
Do an in-place shuffle on an array of numbers. It's trickier than you might think! keep reading »
Write a function to tell us if cafe customer orders are served in the same order they're paid for. keep reading »
Given a 7-sided die, make a 5-sided die. keep reading »
Given a 5-sided die, make a 7-sided die. keep reading »
Your friend copied a bunch of your files and put them in random places around your hard drive. Write a function to undo the damage. keep reading »
Find the area of overlap between two rectangles. In the name of love. keep reading »
Write code to continually track the max, min, mean, and mode as new numbers are inserted into a tracker class. keep reading »
A building has 100 floors. Figure out the highest floor an egg can be dropped from without breaking. keep reading »
Figure out the optimal buy and sell time for a given stock, given its prices yesterday. keep reading »
Design a ticket sales site, like Ticketmaster keep reading »