Found 96 Articles for Campus Interview

Java Program to Get the middle element of LinkedList in a single iteration

AmitDiwan
Updated on 30-Mar-2022 08:13:49

129 Views

In this article, we will understand how to get the middle element of linkedList in a single iteration. The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.Below is a demonstration of the same −Suppose our input is −Input linked list: 100 200 330The desired output would be −The middle element of the list is: 200AlgorithmStep 1 - START Step 2 - Declare a LinkedList namely input_list. Declare five node objects namely head, first_node, second_node, ... Read More

Java Program to Implement the queue data structure

AmitDiwan
Updated on 30-Mar-2022 08:01:28

2K+ Views

In this article, we will understand how to implement the queue data structure. A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).Below is a demonstration of the same −Suppose our input is −Input Queue: [150, 300, 450, 600]The desired output would be −After removing an element, the elements of the queue are: [300, 450, 600]AlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Add elements to it using the ‘offer’ method. Step 4 - Display the queue content Step 5 - ... Read More

Java Program to Implement LinkedList

AmitDiwan
Updated on 30-Mar-2022 07:50:48

317 Views

In this article, we will understand how to implement linked-list. The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The elements of the linked list are: 100 150 200 250AlgorithmStep 1 - START Step 2 - Create a class with the required members. Step 3 - Define an ‘insert’ function to add elements to the list. Step ... Read More

Java Program to Implement the graph data structure

AmitDiwan
Updated on 30-Mar-2022 07:45:08

747 Views

In this article, we will understand how to implement the graph data structure. we implement the graph data structure we implement graphs in Java using HashMap collection. HashMap elements are in the form of key-value pairs. We can represent the graph adjacency list in a HashMap.Below is a demonstration of the same −Suppose our input is −Number of Vertices: 5 Number of edges: 5The desired output would be −The connections between the nodes of the Graph are: 1 - 2 1 - 3 1 - 4 2 - 4 2 - 5 3 - 4 3 - 5 4 - ... Read More

Java Program to Sort ArrayList of Custom Objects by Property

AmitDiwan
Updated on 30-Mar-2022 07:40:55

550 Views

In this article, we will understand how to sort arrayList of custom objects by property. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.Below is a demonstration of the same −Suppose our input is −The list is defined as Java Scala Python MysqlThe desired output would be −The list after sorting values: Java Mysql Python ScalaAlgorithmStep 1 - START Step 2 - Declare namely ... Read More

Java Program to Perform the inorder tree traversal

AmitDiwan
Updated on 30-Mar-2022 07:39:14

571 Views

In this article, we will understand how to perform the inorder tree traversal. In InOrder traversal, each node is processed between subtrees.In simpler words, visit left subtree, node and then right subtree.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The In-Order traversal of the tree_object is: 5->12->6->1->9->AlgorithmStep 1 - START Step 2 - A class with the data specifications is previously defined. Step 3 - Create a new instance of the class. Step 4 - Initialize the instance with relevant values. Step 5 - Invoke the method to perform inorder ... Read More

Java Program to Sort a Map By Values

AmitDiwan
Updated on 30-Mar-2022 07:33:04

301 Views

In this article, we will understand how to sort a map by values. Java HashMap is a hash tablebased implementation of Java's Map interface. It is a collection of key-value pairs.Below is a demonstration of the same −Suppose our input is −Input HashMap: Key = Java, Value = 45 Key = Scala, Value = 20 Key = Mysql, Value = 11 Key = Python, Value = 75The desired output would be −The HashMap after sorting is: Key = Mysql, Value = 11 Key = Scala, Value = 20 Key = Java, Value = 45 Key = Python, Value = 75AlgorithmStep ... Read More

Java Program to Merge two lists

AmitDiwan
Updated on 30-Mar-2022 07:32:25

321 Views

In this article, we will understand how to merge two lists. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements.Below is a demonstration of the same −Suppose our input is −First list: [45, 60, 95] Second list: [105, 120]The desired output would be −The list after merging the two lists: [45, 60, 95, 105, 120]AlgorithmStep 1 - START Step 2 - Declare three integer lists namely input_list_1, input_list_2 and result_list. Step 3 - Define ... Read More

Java Program to Find a Sublist in a List

AmitDiwan
Updated on 30-Mar-2022 07:28:28

190 Views

In this article, we will understand how to find a sublist in a list. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements. A part or a subset of list is called sublist.Below is a demonstration of the same −Suppose our input is −Input list: [101, 102, 103, 104, 105, 106, 107, 108, 109] Start Index: 3 End input: 6The desired output would be −The Elements from 3 index position to 6 index position ... Read More

Java Program to Iterate through Elements of HashMap

AmitDiwan
Updated on 30-Mar-2022 07:27:54

278 Views

In this article, we will understand how to iterate through elements of hash-map. Java HashMap is a hash table based implementation of Java's Map interface. It is a collection of key-value pairs.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The elements of the HashMap are: 1 : Java 2 : Python 3 : Scala 4 : JavascriptAlgorithmStep 1 - START Step 2 - Declare a HashMap namely input_map. Step 3 - Define the values. Step 4 - Iterate using a for-loop, use the getKey() and getValue() functions to fetch the ... Read More

Advertisements