MockQuestions

Java Beginner Level Algorithms Mock Interview

To help you prepare for your Java Beginner Level Algorithms interview, here are 30 interview questions and answer examples.

Get More Information About Our Java Beginner Level Algorithms Interview Questions

Question 1 of 30

How do you implement a stack by using an array?

This question tests your understanding of both an array and stack data structures.

Stack is a Last-In-First-Out (LIFO) data structure. All element operations are on one end of the stack, called the stack top. An entry can not be reached as long as there are entries on top of it, i.e. entries inserted after itself.

An array can be used by adding elements to its consecutive cells starting from its first cell at index 0. In such an implementation, a pointer variable, say `stackTop`, should be maintained always to point to the next available cell, i.e. the index that is 1 greater than the index of last element inserted to the stack. So by this, all array cells with indexes less than stackTop hold entries in them, and all those with greater indexes are empty.

The following code shows the overall logic of the 3 main stack operations. The boilerplate code to cover the edge cases that the stack is full or empty are omitted.

Object[] stack = new Object[MAX_STACK_SIZE]; 
int stackTop = 0;

When a new entry is made to the stack, i.e. `push(entry)` operation, it is placed at the cell that `stackTop` currently shows. The `stackTop` value is incremented after that:

stack[stackTop++]=entry;

`pop()` operation removes and returns the element at the top of the stack. It is the logical inverse of `push()`:

return stack[--stackTop];

`peek()` returns the top element like `pop()` does, but without removing it:

return stack[stackTop];

Each of these operations have the complexity O(1), which is the complexity of core stack data structure. O(1) is called constant time. The time taken is not dependent on the size of the input.

Written by on May 4th, 2021

Next Question

30 Java Beginner Level Algorithms Interview Questions & Answers

Below is a list of our Java Beginner Level Algorithms interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. How do you implement a stack by using an array?

  • 2. A binary tree is of height K and has N leaf nodes on it. What are the maximum and minimum possible values of N in terms of K? Would your answer be any different if the tree was a binary search tree?

  • 3. What is an in-place algorithm? Can you give an example?

  • 4. What can you say about the following statement? Is it any necessary to obey this rule?

  • 5. What is a collision? What happens when there is a collision in HashMap?

  • 6. Can you store a duplicate Value in a <Key,Value> entry pair of HashMap?

  • 7. What is the difference between binary tree and balanced binary tree?

  • 8. Can you store a duplicate key in `HashMap`?

  • 9. Which of the following 3 data structures is more memory efficient than the others - HashMap, ArrayList, LinkedList?

  • 10. What are the elements in `myStack` after the following code is executed?

  • 11. Which of the following Java data structure(s) are backed by an array - LinkedList, ArrayList, HashMap?

  • 12. Which of the following computational complexities are polynomial - O(1/n), O(n!), O(n^2), O(log n), O(2^n), O(n^n)?

  • 13. You will be using a data structure to store sorted integers and conduct binary search on them. Which of the following would you use and why: ArrayList, LinkedList, or HashSet?

  • 14. When do you prefer to use TreeSet over HashSet? Explain.

  • 15. What are the basic interfaces of Java Collections framework?

  • 16. Suppose you have the following array of length 16. How would you increase the array's length to 32?

  • 17. What does the following method do? Can you write it by using an array instead of a HashSet?

  • 18. What does `myMethod` do? Can you write its non-recursive version?

  • 19. Suppose T is a full tree. Which of Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms traverse a leaf of T soonest?

  • 20. What is the complexity of an optimum algorithm to sort `n` integer values that are greater than 0 and l*ess than 100?

  • 21. What is the complexity of an optimum algorithm to sort `n` arbitrary integer values?

  • 22. Write a recursive method that returns true if the entries of a character array (char[]) constitute a palindrome, false otherwise.

  • 23. What is the computational complexity of `contains()` operation (checking whether it contains an object among its elements) on an `ArrayList`? Would your answer be any different for a `LinkedList`?

  • 24. Which of the following two operations are faster in a LinkedList than on a TreeSet - `add(int)`, `contains(Object)`, `remove(Object)`? Explain why.

  • 25. Suppose `aColln` is a collection with the following two integers in it: 5, 7. How many elements are there in `mySet` and `myList` after the following code is executed?

  • 26. How many different paths can exist between a pair of nodes in a binary tree?

  • 27. Which of the following 4 operations is faster on a HashSet<String> than on a LinkedList<String>?

  • 28. What is the computational complexity of get(int) operation (accessing an array element by its position) on an `ArrayList`? Would your answer be any different for a `LinkedList`?

  • 29. What is the difference between a binary tree and a binary search tree?

  • 30. Take the following code: How many entries are there in myMap after this code is executed?