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. … Read more ›...
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. … Read more ›...
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.
|
1 2 3 4 5 6 7 |
package com.mycompany.flatten; public interface FlattenTree<T> { void flatten(Tree<T> tree); } |
Step 2: The iterative implementation … Read more ›...
#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 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,
…
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...
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. … Read more ›...
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 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package linked.list; public class LinkedListTest { public static void main(String args[]) { SinglyLinkedList<Integer> linkedList = new SinglyLinkedList<Integer>(); linkedList.add(1); linkedList.add(2); linkedList.add(3); printMiddleNodeInsinglePass(linkedList); } public static void printMiddleNodeInsinglePass(SinglyLinkedList<Integer> linkedList) { Node<Integer> current = linkedList.getHead(); int length = 0; Node<Integer> middle = linkedList.getHead(); //Loop until last element is reached while (current.getNext() != null) { length++; if (length % 2 == 0) { middle = middle.getNext(); } current = current.getNext(); } if (length % 2 == 1) { middle = middle.getNext(); } System.out.println("length of SinglyLinkedList: " + (length+1)); System.out.println("middle element of SinglyLinkedList : " + middle); } } |
Output:
|
1 2 3 |
length of SinglyLinkedList: 3 middle element of SinglyLinkedList : data=2, next=data=3, next=null |
… Read more ›...
Q. Write a method which takes the parameters (int[ ] inputNumbers, int sum) and checks input to find the pair of integer values which totals to the sum. If found returns true, else returns false? Considerations: Should it work for negative integers? … Read more ›...
Q. Java does not have a Graph implementation, how would you go about implementing your own? A. Graphs are data structures that represent arbitrary relationships between members of any data sets that can be represented as networks of nodes and edges. A tree structure is essentially a more organized graph...
Q. Java does not have a Graph implementation, how would you go about implementing your own? A. Graphs are data structures that represent arbitrary relationships between members of any data sets that can be represented as networks of nodes and edges. A tree structure is essentially a more organized graph...
Q. If Java did not have a HashMap implementation, how would you go about writing your own one? A. Writing a HashMap is not a trivial task. It is also not a good practice to reinvent the wheel. The interviewer is trying to evaluate your level of technical knowledge and...
Q. Can you write a code to search for number 5 in 7 3 6 8 2 9 5 4? A. The code below uses the linear search algorithm. The linear search algorithm’s two advantages are simplicity and the ability to search either sorted or unsorted one-dimensional arrays. … Read...
Q. How to create a LinkedList from scratch A. Adding Removing Step 1: The node that stores the data and the reference to the next Node. … Read more ›...
I have a good news, I got job offer. I like to share with you from my bottom of my heart feeling thankful to you.