Given an array of integers, return indices of two numbers such that they add up to a specific target.
For example: Given [ 1, 2, 3, 4, 5, 6 ] and a target 9, return [ 2, 5 ]
Can you solve this question without help? Known as the 2sum problem, it’s considered an easy problem that an interviewee should be able write a solution to without much difficulty. (Stumped? The solution is explained here.)
Most large tech companies will give applicants at least one high-pressure technical interview where you’re expected to solve difficult algorithm or data structure problems under time pressure. The algorithmic interview is the hardest kind of interview you’ll face. There are two ways algorithmic interviews are given:
Coding challenges involve solving problems under time pressure. Applicants are given a customized link to a code editor with one or a few problems, which closes after a set amount of time – generally between 30 minutes and two hours. The company sees the final code that was submitted, as well as how many of the test cases passed. Code challenges are generally a screening step; applicants with promising resumes are given the coding challenge, and those who score highly advance to the next stage in the interviewing process.
Live interviews up the pressure by having applicants solve the problem in front of an interviewer. They can take place in person (also called “whiteboarding”), over the phone, or over video chat. Applicants write code in a shared code editor and explain what they’re doing to the interviewer, who takes notes and occasionally gives hints if they’re in a good mood. The applicant’s code is evaluated, but the interviewer also pays attention to his explanations of what he’s doing and how he approaches the problem.
Before you work on the type of challenging problems that you might see an interview, it’s important to make sure that you have a good grasp of the material itself.
Ideally, you took a data structures and algorithms courses in college. If you didn’t, or it wasn’t a good course, or you’ve forgotten the material, there are a variety of books, websites, and online courses that will teach you. The following are two options, but you can find literally millions of others by asking around and using Google.
- Data Structures & Algorithms in Java by Robert Lafore [book]
- Data Structures and Algorithms Specialization [online course]
Data structures you must know:
Algorithms you must know:
- Sorting algorithms: quicksort and mergesort at minimum
- Graph traversal algorithms: Depth First Search (DFS), Breadth First Search (BFS), Dijkstra’s algorithm
- Search algorithms: binary search
- Knapsack problems
- Recursion and dynamic programming (DP)
You also must must know Big-O Notation so that you can evaluate the complexity of your algorithms. You will always be expected to compare the complexities of different solutions to a problem in an interview.
Unless you’re applying for a specialized position (graphics programming, for example) you generally won’t be asked advanced data structures like splay trees or A* search.
Once you have the material straight, the next step is to get used to solving interview-style problems. Some people find it easier to work with a book and others find it easier to solve problems online. Neither is a better technique than the other – it depends most on how you learn. The most important thing is to be consistent: whatever you use, go through it completely. Nothing is less productive than constantly getting into new books or websites and then dropping them days or weeks later.
There are two main books that people use to study for algorithm interviews.
- Cracking The Coding Interview by Gayle Laakmann McDowell is a great guide to the interview process. McDowell worked at Google, Microsoft, and Apple, and explains what interviewers want to see in an extremely clear fashion. (You might see this book referred to as “CTCI” around the internet.) The book goes topic by topic and explains each topic and how to approach it, and provides interview questions on each topic whose solutions are in the back. I highly, highly recommend Cracking The Coding Interview. Besides for covering most of what you’ll need for interviews, it’s written in a concise, clear, and friendly tone.
- The Algorithm Design Manual by Professor Steven Skiena of Stony Brook University is the other recommended book to learn how to understand algorithms. It’s a good way to understand how to approach problems, and how to recognize which algorithms can be use to solve a given problem. If you aim to learn how to think algorithmically and understand algorithms on a deeper level, The Algorithm Design Manual is the book for you.
One of the biggest benefits of using websites is that you can see how others solve the problems, and read discussions about the advantages and disadvantages of different solutions. You can also try out your own solutions to problems and see if they work against test cases.
There are many sites out there, but I’m covering LeetCode at length because that’s what I used and was successful with.
- LeetCode is the main site, so much so that the process of studying for tech interviews is sometimes referred to as “grinding LeetCode.” There is a great guide to using LeetCode here. Some other useful components of LeetCode:
- LeetCode Premium is one of the interviewing secrets of the tech world. You can see what companies ask which questions, with very high accuracy in my experience. It’s expensive though, at $39/month at the time of this writing.
- LeetCode Discuss is where people talk about their experiences interviewing at different companies. It can be very helpful to see what process and types of questions to expect at a specific company.
Other sites that provide practice problems, review, and communities include InterviewCake, CodeWars, HackerRank, CareerCup, and HackerEarth. I’ve never used these, but feel free to discuss them in the comments if you have.
Solving Problems Under Pressure
When I got good at problem solving, I still kept failing interviews. My issue was that I would freeze if I didn’t know how to solve a problem off the bat. If the problem I was presented with wasn’t a question I recognized, my brain would shut down and I couldn’t strategize and figure out the solution.
What cleared that issue up for me was Pramp. Pramp is a wonderful free service that pairs you up for an hour with someone else who is practicing interviewing. The other person interviews you for half an hour, and then you interview them for the other half an hour.
Pramp very realistically replicates the interviewing environment, and after about ten or so Pramp interviews I found that I was able to stay very calm and think straight during interviews.
Another benefit of Pramp is that you see what it’s like to sit in the interviewer’s chair. Once you understand that, you’ll begin to understand how to really optimize your explanations of what you’re doing in an interview.
- Choose one programming language that will be your interviewing language. Know it inside and out; know its quirks and edge cases and what it does when you multiply a char by a decimal. Always use that language for both practice problems and real interviews. It’s best to choose a language that you can always interview in, such as C++, Java, or Python.
- Always evaluate the Big-O complexity of your solutions. An interviewer will always expect you to do this, and will ask you for it if you don’t.
- You can do it. Some people start trying to solve LeetCode problems, realize they have no idea where to start, and give up, rationalizing that they “aren’t good at algorithms.” Anyone can succeed at these interviews – it just takes a lot of study and practice. Granted, some people probably have an easier time than others, but once you study and learn to recognize the common problems given you’ll be surprised at how much easier the questions have gotten.
Practicing for algorithmic interviews is hard, and can take a lot of blood, sweat, and tears. However, the returns are worth it. Besides for getting good at interviews, having a good grasp on data structures and algorithms can help you immeasurably in your career, both when using them and when understanding larger, more advanced ideas in tech.