LinkedList Programming Interview Questions

A linked list is another important data structure after array and String. It actually compliments array and whatever you cannot do with an array, you can do with a linked list.

For example, the array needs contiguous memory to store objects but the linked list doesn’t need that. It’s difficult to add and remove elements in an array because you need to shift existing elements but that is very easy with a linked list, as you just need to change the pointer to accommodate them.

But, nothing is free in this world. While linked list provides all these functionalities but the cost of that you lose the ability to search elements in constant time with index. Searching and element require traversing linked list, which means examining all nodes, thus cost around O(n) time.

14) How do you find middle element of a linked list in a single pass?
To answer this programming question I would say you start with a simple solution on which you traverse the LinkedList until you find the tail of linked list where it points to null to find the length of the linked list and then reiterating till middle.

After this answer interviewer will ask you to find the middle element in single pass and there you can explain that by doing space-time trade-off you can use two pointers one incrementing one step at a time and other incrementing two-step a time, so when the first pointer reaches end of linked second pointer will point to the middle element.

15) How do you find the 3rd element from last in a single pass? (solution)
This programming question is similar to above and can be solved by using 2 pointers, start the second pointer when the first pointer reaches third place.

16) How do you find if there is any loop in a singly linked list? How do you find the start of the loop? (solution)
This programming question can also be solved using 2 pointers and if you increase one pointer one step at a time and other as two steps at a time they will meet in some point if there is a loop.

17) How do you reverse a singly linked list? (solution)

18) Difference between a linked list and array data structure? (answer)

If you are having trouble solving these linked list coding questions then I suggest you refresh your data structure and algorithms skill by going through Data Structures and Algorithms: Deep Dive Using Java course.

If you need more linked list based questions then you can also check out this list of 30 linked list interview questions for more practice questions.

Binary Tree Programming Interview Questions

Binary tree or simply tree is one of favorite topic for most of the interviewer and pose a real challenge if you struggle with recursion. Programming questions on the tree can become increasingly difficult when you think iterative but sometimes can be very easy if you come with a recursive solution.

18) How do you find the depth of a binary tree? (solution)

19) Write code to print InOrder traversal of a tree? (solution)

20) Print out all leaf node of a binary tree? (solution)

21) Write a method in Java to check if a tree is a binary search tree or not? (solution)

22) How to check if a tree is balanced or not in Java? (solution)

23) How is a binary search tree implemented? (solution)

24) How do you perform preorder traversal in a given binary tree? (solution)

25) How do you traverse a given binary tree in preorder without recursion? (solution)

26) How do you print all nodes of a given binary tree using inorder traversal without recursion? (solution)
27) How do you implement a postorder traversal algorithm? (solution)
28) How do you traverse a binary tree in postorder traversal without recursion? (solution)
29) How are all leaves of a binary search tree printed? (solution)
40) How do you count a number of leaf nodes in a given binary tree? (solution)
41) How do you perform a binary search in a given array? (solution)

Binary tree based questions sometimes get trick and if you are having trouble solving these tree-based list coding questions then I suggest you revise your data structure and algorithms skill by going through From 0 to 1: Data Structures & Algorithms in Java course.

Top 30 Coding questions asked in Interview - Java C C++ Answers

It’s written by an ex-Googler and it is one of the most comprehensive course to revise all important data structures like an array, linked list, binary tree etc.

Programming Questions on Searching and Sorting

I have only included two programming questions related to searching and sorting but there are more can be found on Google. Purpose of these programming questions is to see whether a programmer is familiar with the essential search and sort mechanism or not.

23) Write a program to sort numbers in place using quick sort? (solution)

24) Write a program to implement a binary search algorithm in Java or C++? (solution)

25) How do you sort Java object using Comparator? (answer)
This is another Java specific programming questions and you can check how to sort Object using Comparator and Comparable for an answer.

26) Write code to implement Insertion Sort in Java? (solution)

27) Write code to implement Bubble Sort in Java? (solution)

If you can solve these questions easily then you are in good shape. For more advanced questions, I suggest you solve problems given in the Algorithm Design Manual by Steven Skiena, a book with the toughest algorithm questions.

Top 30 Algorithms questions asked in Interview - Java C C++ Answers

Programming Questions on Numbers

Most of the programming questions are based on numbers and these are the ones which most of us did on college level and mind you they still has value I have seen programmers with experience of 3 years struggle with these programming questions and doesn’t solve it some time and take a lot of time which simply shows that they are not in programming in there day to day work.

26) Write code to check whether a no is a power of two or not? (solution)

27) Write a program to check whether a number is a palindrome or not? (solution)
Check out this post which shows how to reverse a number in Java and can be used to find if its palindrome or not.

28) Write code to check whether an integer is Armstrong number or not? (solution)
Here is a Java program to find Armstrong number, you can use the same logic to write code in any other programming language like C and C++.

29) Write a program to find all prime number up to a given number? (solution)
Here is another Java program to find prime numbers and print them. By using logic demonstrated in this program; you can write a similar program in C and C++.

30) Write a function to compute Nth Fibonacci number? Both iterative and recursive? (solution)
You can check this Java program to print Fibonacci Series using recursion and iteration.

31) How to check if a number is binary? (solution)
For this question, you need to write a function which will accept an integer and return true if it contains only 0 and 1 e.g. if the input is 123 then your function will return false, for 101 it should return true.

32)  How to reverse an integer in Java? (solution)

33) How to count a number of set bits in given integer? (solution)

34) How to find the sum of digits of a number using recursion? (solution)

35) How to swap two numbers without using temp variable? (solution)

36) How to find the largest of three integers in Java? (solution)

37) Write a program to find prime factors of an integer? (solution)

38) How to add two integers without using arithmetic operator? (solution)

If you need more such coding questions you can take help from books like Cracking Coding Interview, which presents 189+ Programming questions and solution. A good book to prepare for programming job interviews in a short time.

Top 30 Programming Interview Questions Answers for Programmers

General Programming Interview Questions

In this category of programming questions, I have put questions which are not fit into any data structure but presents a real-life problem and you need to provide a solution. These programming questions are sometimes based on problems faced by the developer itself.

I have not included many Software design-related programming question which I have shared on Top 20 software design questions and answers; you can also check that.

31) Write a program to find out if two rectangles R1 and R2 are overlapping? (solution)

32) You need to write a function to climb n steps you can climb either 1 step at a time or 2 steps a time, write a function to return a number of ways to climb a ladder with n step. (solution)
It’s actually a Fibonacci series so you can solve it like that.

33) Write code for Generate Random No in a range from min to max? (solution)

34) Write a program for word-wrap which should work on any screen size? (solution)

35) Design an algorithm to find the frequency of occurrence of a word in an article? (solution)

36) Write a program to implement a blocking queue in Java? (solution)

37) Write a program for the producer-consumer problem? (solution)
This article solves the producer-consumer problem using BlockingQueue in Java. You can refer it to answer this question.

Books to prepare for Programming Job Interviews

There are a lot of good books available, which can help the programmer to do well on Interviews. Here is a list of book, which I personally prefer, in the order, I like them.

Programming Interview Questions and Answers

A must read books for both beginners and experienced programmer alike. It not only help you to do well on interviews but also on negotiation, answering general questions etc.

This book contains a collection of questions from a wide range of programming topics, including data structure, algorithms, strings, Java, networking, database, SQL, object-oriented programming, software design etc. This book will give you the whole picture of what can be asked.

3. Top 10 coding interview problems asked in Google with solutions: Algorithmic Approach
This is the must read a book, if you are preparing for Google interview, or something along the line e.g. Facebook, Amazon or Microsoft Interviews. It contains top 10 programming problems, frequently asked at Google with detailed worked out a solution, explanation in both pseudocodes and in C++.

Tips on answering Programming questions

Interviews are not ready and even if you know the answers you need to keep some things in mind while answering the questions or solving problems. Interviewer often likes to see your ability to solve unknown problems and how you react when a new challenge presented.

For example, if you wrote recursive solution then they will ask you to solve without recursion, if you use additional memory then you will ask you to solve without that and in-place, mostly in case of an array and linked list problems.

Here are some of the tips to do well on your programming interview:

1. If Interviewer asks you to write function then make sure you do some necessary check for bad input e.g. null check or empty check. Most of the time programmer forgets to test for not null, empty, less than 1, greater than 1 or zero input.

2. If you write an iterative version of function then Interviewer may ask you to write recursive version or vice-versa so be prepared for that.

3. If you write a recursive function then Interviewer will ask to optimize it, even in case of Iterative version. So remember that you can optimize recursive function by Memorization (caching already calculated value) and by applying some space/time tradeoff principle. For example, recursive version of Fibonacci series has O(n ^2) time performance which can be reduced to O(n) using Memoziation.

4. The interviewer may ask you to calculate Order of complexity for best and worst case of any method so be prepared.

5. Most of the time Interviewer ask how to fix a problem as follow-up question e.g. he will ask how do you find deadlock and then how to fix deadlock in java etc.

These are just some of the tips you can follow to be successful in you programming Job interviews. As I told, it’s not enough just answering the questions, you need to be attentive and see how the interviewer is reacting. Is he getting pleased with your approach you are not irritating him off by asking silly questions, you need to be attentive to your surrounding?

If you are a fresher or a junior developer and haven’t had much programming job interview experience, I suggest you go through Preparing For a Job Interview course by John Sonmez, one of my favorite author and instructor when it comes to career guidance.

Apart from these programming interview questions here are some of other Java programming questions I have already discussed in my blog.

You can also take help from  Programming Interviews Exposed and Cracking the Coding Interview book with a solution to prepare for any programming Job interview. Those two books have helped me a lot in the past and even today I read them whenever I need to refresh my concepts.

Now You’re Ready for the Programming Interview

These are some of the most common questions outside of data structure and algorithms that help you to do really well in your interview.

I have also shared a lot of these questions on my blog, so if you are really interested, you can always search for them.

These common coding, data structure, and algorithm questions are the ones you need to know to successfully interview with any company, big or small, for any level of programming job.

If you are looking for a programming or software development job, you can start your preparation with this list of coding questions.

This list provides good topics to prepare and also helps assess your preparation to find out your areas of strength and weakness.

As I said before, a good knowledge of data structure and algorithms is important for success in coding interviews and that’s where you should focus most of your attention.

Closing Notes
Thanks, You made it to the end of the article … Good luck with your programming interview! It’s certainly not going to be easy, but by following this roadmap and guide, you are one step closer to getting the job you always wanted.

If you like this article, then please share with your friends and colleagues, and don’t forget to follow javinpaul on Twitter!

Source Article