Blog Archives
1 2 3 4

00: Tree from a list & flattening it back to a list in Java

Hierarchical data with parent & child relationships are very common, and Java collection does not have a Tree data structure, hence it is a popular interview question. Further questions include tree data structure processing using recursiion &

Read more ›



01: Can you design classes & interfaces using OO?

Q1. How would you go about designing a “farm animals” application where animals like cow, pig, horse, etc move from a barn to pasture, a stable to paddock, … Read...



01: String parsing & date formatting – complete the given code

Q. Complete the method “changeDateFormat(String paragraph)” which takes a string input containing dates in mm/dd/yyyy format and converts them to “dd/mm/yyyy” format. For example, “My birthday is on 04/25/1980

Read more ›



01: Tree flattening example in Java

Q. Traverse the tree in depth and return a list of all passed nodes. It should handle different data types as Double, String, etc. The4 interface for the tree node...



01: What is wrong with following Java code snippets? Auto-unboxing, switch, short circuit & exception

Auto-unboxing, switch, short circuit logical operators & exception handling are core Java basics that sometimes lead to obscure errors if not understood well & often tested in coding tests. Q1....



02: Scenarios based Java OO concepts & GoF design patterns – naive Template Method pattern

Extends Can you design classes & interfaces using OO?. Only experience, and asking the right questions can teach you what design pattern to use, in which scenario.

Read more ›



02: Tree structure – calculate average recursively & iteratively in Java

Q. Complete the following “TreeProcessingImpl” class ….. so that the unit tests shown below pass? Skeleton Code Unit Tests A. … Read more ›...



02: Working with Java Collections – Complete the given code

Q. Complete the method “canAttendAll(Collection seminars)” which takes a collection of seminar objects with start and end timestamps, and the method should determine if there is an overlap in seminars...



03: Scenarios based Java OO concepts & GoF design patterns – Strategy Pattern

The following post improves on the design flaws listed in the previous post entitled 02: Scenarios based Java OO design & GoF design patterns – naive Template Method pattern. …...



03: Tree navigation – is a BST Tree?

Q. Complete the method “isValidBST(Node root)” which takes a “Tree node” as an input to evaluate if the input is a valid BST (i.e. Binary Search Tree)? … Read more...



03: Tree traversal algorithms in Java

Q1. What are the different binary tree traversal mechanisms? A1. Traversing a tree means visiting all the nodes of a tree in order. … Read more ›...



03: Understanding Big O notations

Q. Have you seen job advertisements requiring Java candidates to work in real-time or high volume transaction processing systems? If you are applying for such jobs, … Read more ›...



03: What is wrong with this code? Data types & equals( ) Vs, hashCode( ) contract

Q6. What is wring with this code? A6. Infinite loop at due to sum != 1.0. Using floating point variables like float or double in loops to compare for equality...



04: 7 Java Collection Quizzes

Q1. When is an object needs to implement the Comparable interface? a) When adding it to a HashSet. … Read more ›...



04: Are Anagrams – Complete the given code

Q. Complete the “areAnagrams(String a, String b)” method so that it returns if two given input strings are anagram or not? A. … Read more ›...



04: Convert Lists to Maps and Maps to Lists with Java 8 power

Why convert from a list to a map? Maps allow you to store and access data by a “KEY”. Lists only allow you to access data by either index or...



04: Tree – Get root to leaf max sum in Java example

Q. Write code to traverse a tree and get the largest “root-to-leaf sum”? A “root-to-leaf sum” is a sum of the node values in a root-to-leaf path. … Read more...



04: What is wrong with this code? Java Collection & ConcurrentModificationException

Q9. What is wring with this code? A9. throws a Runtime exception called “java.util.ConcurrentModificationException” The output will be: Fix 1: Use iterator.remove() instead of collection.remove Output: Fix 2: Use java.util.…...



05: Finding the 2nd highest number in an array in Java

Requirements gathering Does the array allow duplicates? If duplicates are allowed, then do you need to report duplicates? For example, in {2,4, 6, 3, 6, 5}, is 6 or 5...



05: Is a palindrome – complete the given code

Q. Complete the method “isPalindrome(String str) ” which takes a “string” as an input to evaluate if the input is a palindrome? A. … Read more ›...



05: Is it a valid binary search tree (BST) ?

Q. Write code test if a given tree is a binary search tree (i.e. BST)? A. A binary search tree (BST) is a binary tree where each node satisfies the...



05: What is wrong with this code? Deep Vs Shallow comparison & multithreading

Q10 What is wring with this code? A10. date1 == date2 only compares the references and not the actual dates. Since both the references are different objects, … Read more...



06: Binary Tree – Sum Levels Java Example

Q. Write code to traverse a tree and return the sum of the values (Node.getValue()) of all nodes at the level N in the binary tree? … Read more ›...



06: Binary Tree – Get root to leaf max sum in Java example

Q. Write code to traverse a binary tree and get the largest “root-to-leaf sum”? A “root-to-leaf sum” is a sum of the node values in a root-to-leaf path. … Read...



06: Change Dir Path – String parsing & working with collections

Q. Complete the method ” public DirPath cd(String newPath)” which applies the following rules: /   : starting with “/” means root directory, … Read more ›...



06: What is Wrong with this code? Multithreading & wait/notify

An excellent written test question to assess your Java multi-threading knowledge. Please practice it by doing it yourself.

Q. Review the code shown below and then answer the following questions.

Read more ›



07: Index of long consecutive characters in a string – flow control

Q. Complete the method “int startIndexOfLongestRun(String input)” that returns the start index of a character that has the longest consecutive count. A. … Read more ›...



07: Tree – Sum Levels Java example

Q. Write code to traverse a tree and return the sum of the values (Node.getValue()) of all nodes at the level N in the tree? A. … Read more ›...



07: What is wrong with this code? Multithreading & locks

Q. What is wrong with the following Java code? Explain what does the following code do? Explain if there is anything wrong with the following code? … Read more ›...



08: Jumping Frog problem in Java – Complete the given code

Q. Complete the method “int numberOfWays(int n)” where n is the distance to cover and the method should return the number of possible combinations if a frog can jump 1...



1 2 3 4

800+ Java Interview Q&As

Top