Blog Archives
1 2

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 & iteration. A Tree data structure can be represented as shown below.

Step 1: A simple Node.java class that encapsulates its value, parent node id & if any child nodes. It is important that “parent” is not included in the toString(..) method as it leads to Stack Overflow error due to recursive calls.

Tags:

01: Tree flattening example in Java with Generics

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

A. Tree storing numbers: Tree storing text: #1. Define the …



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



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. Many different binary tree algorithms involve traversals. For example, if you wish to count the number of employees in an organizational chart you must visit each node….



03: Understanding Big O notations with Java

Q1.  Have  you seen job advertisements requiring Java candidates to work in real-time or high volume transaction processing systems? A1.  If you are applying for such jobs, you can be quizzed on Big O notation. Here are some basics to brush up on. Big O gives you the upper bound. For example,…



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.

A. Let’s have a basic implementation of the above Node interface.



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 following conditions: 1) key in any node is larger than the keys in all nodes in that node’s left subtree…



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?

A. Let’s have a basic implementation of the above Node interface.



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.

A. Let’s have a basic implementation of the above binary tree Node interface.



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. Let’s have a basic implementation of the above Node interface.



08: Part-1: Basic Java Tree structure interview questions and coding questions

Java does not have a Tree class but you can define one. In my 10+ years as a Java developer, I have rarely used a Tree structure,  but when I do use it I find it to be a bit more complicated than working with other data structure. I have…



09: Part 2: Java Tree structure interview questions and coding questions

In the previous post entitled  Java Tree structure interview questions and coding questions — Part 1, we looked at a very simple Tree structure. In this example, let’s look at a more complex tree structure with generics. Q. Write classes for a Tripple tree structue as shown below? The Leaf…



09: When to use which Java data structure? and why?

List, Set, Map, and Queue(access the ends FIFO or LIFO) are the basic Java data structure interfaces for which there are different implementations to cater for different usage patterns. Java data structure interview questions are very popular, and pays to know the differences. #1 Array (Employee[]) Vs List (List<Employee>):  Array…



10: Part-3: Java Tree structure interview and coding questions

This is an extension to Java Tree structure interview and coding questions — Part 2, and adds functional programming and recursion. Step 1: The Tree interface with get( ) method that returns either a Triple tree or Leaf data.

Step 2: The Leaf that implements the Tree interface. …



11: Part 4: Java Tree structure interview and coding questions

This is an extension to Java Tree structure interview questions and coding questions — Part 3 where recursion was used for FlattenTree interface. Let’s use iteration here. Step 1: The FlattenTree  interface.

Step 2: The iterative implementation IterativeFlattenTree. This uses a LIFO stack to push and pop Tree elements.



9 Java Data structures best practices

#1: Choose the right type of Java data structure based on usage patterns

like fixed size or required to grow, duplicates allowed or not, ordering is required to be maintained or not, traversal is forward only or bi-directional, inserts at the end only or any arbitrary position, more inserts or more reads, concurrently accessed or not, modification is allowed or not, homogeneous or heterogeneous collection, etc. Also, keep multi-threading, atomicity, memory usage and performance considerations discussed earlier in mind.

#2: Favor unmodifiable data structures where applicable

Use an immutable collection (aka unmodifiable collection) if you don’t want to allow accidental …



Beginner array coding problems in Java

More Data Structure and Algorithms Coding Questions and answers in Java.

Q1. Write a program that allows you to create an integer array of 5 elements with the following values: int numbers[ ]={5,2,4,3,1}. The program computes the sum of first 5 elements and stores them at element 6, computes the average and stores it at element 7 and identifies the smallest value from the array and stores it at element 8.

A1. Remember that the arrays are zero based.

Tags: ,

Big O notation questions and answers with Java examples

The Big O notation and Java data structures go hand-in-hand in coding tests & job interviews Q1. What do you know about the big-O notation and can you give some examples with respect to different data structures? A1. The Big-O notation simply describes how well an algorithm scales or performs…



Find 2 numbers from an array that add up to the target

Q1. Given an array of integers, find two numbers such that they add up to a specific target number? For example, Given numbers: {2, 3, 8, 7, 5} Target number: 9 Result: 2 and 7 A1. Solution 1: Store processed numbers in a set.



Find middle element of a LinkedList in one pass Java example

The SinglyLinkedList was created in the post “LinkedList creating from scratch in Java”. This extends “SinglyLinkedList” post. Q. How to find the middle node of a linked list in a single pass? A.



1 2

300+ Java & Big Data FAQs - Quick Prep

Java & Big Data Tutorials

Top