No results found for the specified position. 15 Java Advanced Level Algorithms Interview Questions

MockQuestions

Java Advanced Level Algorithms Mock Interview

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

Get More Information About Our Java Advanced Level Algorithms Interview Questions

Question 1 of 15

What does `myMethod()` in the following code do? Can you modify the code to process a doubly linked list?

class Node {  
		int value; 
		Node next;  // line-3
		String id;

		static void myMethod(Node head) {
			Node nextNode; 
			for (Node n=head; n!=null; ) {
				nextNode = n.next; 
				while (nextNode!=null 
						&& nextNode.next!=null 
						&& nextNode.value==nextNode.next.value) 
					nextNode = nextNode.next;
				if (nextNode==null) 
					n = n.next = null;
				else {  // line-16
					n.next = nextNode.value==n.value?nextNode.next:nextNode;  
					n = n.next;   // line-18
				}
			}
		}

	}

This question tests your understanding of singly- and doubly-linked list data structures and your ability to code their logic in Java.

myMethod() processes the singly-linked list given by its head node in the parameter. myMethod() removes all but one of the nodes in each sequence of consecutive nodes that have the same value in the list.

The following changes make the code process on a doubly linked-list for the same solution:

1. Also declare a previous pointer on class `Node` -- replace line-3 with the following:

Node next, prev; 

2. Also update the `prev` pointer value when a node is removed – insert the following if statement between lines 17 & 18:

	if (n.next!=null)   
			n.next.prev = n;

Written by on May 21st, 2021

Next Question

15 Java Advanced Level Algorithms Interview Questions & Answers

Below is a list of our Java Advanced 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. What does `myMethod()` in the following code do? Can you modify the code to process a doubly linked list?

  • 2. What does the following code do? Can you simplify it?

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

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

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

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

  • 7. Write a method that takes two String parameters and returns true if these two String-s are anagrams of one another. Two strings are anagrams if they have the same number of each character.

  • 8. A String str is a pseudo-palindrome if str is a palindrome when all the 5 vowels of the English alphabet, upper-case and lower-case, are removed from it. Can you write an algorithm that returns True if a String is a pseudo-palindrome?

  • 9. What does myMethod() in the following code do? Can you write its recursive version?

  • 10. In the following code, the class Item implements Comparable for sorting its objects in decreasing order of nOfLikes. Write a Comparator for sorting Item-s in increasing order of their costs. What happens when you use that Comparator for sorting Item-s?

  • 11. What is hashCode() and equals() contract? How can you implement hashCode() for the following equals() implementation?

  • 12. Given the following problem, can you write an algorithm for computing the maximum number of these marbles you can buy with a given K dollars? What is the computational complexity of your algorithm?

  • 13. The following code successfully compiles, but doesn't run. What is wrong with it? Can you fix it?

  • 14. Write a method that prints the number of occurrences of each letter in a given String in the order they occur. Eg. When the String is "abcbb", the output should be "a - 1, b - 3, c - 1". What is the runtime efficiency of your algorithm?

  • 15. Given two sorted integer arrays, find the K-th smallest integer among all in these two arrays combined. What is the complexity of your algorithm?