Skip to main content

Posts

Showing posts from December, 2021

How to Merge two sorted LinkedList Recursively in Java? Explained with Stack Trace

Merging two sorted Linked List using Recursion approach with Stack trace First we will create two sorted Linked list and then merge them using recursion approach. Lets jump on code.  public class MergeSortedLLRecursively {          // Static nested inner class     static class Node {         int data;         Node next;                  Node (int data) {             this.data = data;         }                  public void setData(int data) {             this.data = data;         }                  public void setNext(Node next) {      ...

How to Reverse a String Recursively in Java? | Explained with Stack Trace

Reverse a String Recursively in Java In this article, we will learn how to reverse string using recursive approach with stack trace. As we all know, Recursion is concept where method is calling itself until base case does not satisfy. Example 1 : Reverse string recursively with return import java.util.Scanner; public class ReverseString {     public static void main(String[] args) {                  Scanner sc = new Scanner(System.in);         System.out.println("Enter String for Reverse :");         String str = sc.nextLine();         String reverseStr =    reverseStringByRecursive(str);         System.out.println("Reversed String : \n"+reverseStr);     }          private static String reverseStringByRecursive(String str) {  ...

How to Reverse Linked List in Java? | Iterative and Recursive approach

Reverse Linked List (LL) using Iterative (While loop) and Recursive approach in Java In this article, we will seen both Recursive and Iterative approach for reversing Singly linked list with explanation.    First we store data (value and next node reference) in LinkedList custom class one by one. and then reverse it using loop.  For reversing linked list data, we do not need to swap any elements we just need to change next data for particular Node.  Lets jump to code for better understanding. Example 1 : Iterative (Loop) approach for reverse Linked List class Node {         int data;         Node next;          // Constructor for initialize data     public Node (int data) {             this.data = data;             this.next = null;     ...

Frequently asked interview questions and answeres for Java Engineers?

Most asked interview questions and answers for Java developers on Core Java 1. Which are OOPs concept in Java? Class Object Abstraction Encapsulation Inheritance Polymorphism    Learn more about above concept with example : Object Oriented Programming (OOP) concept in Java with Examples   2. What contain Real-world object? Real-worls object contains State and Behavior . 3. What is Wrapper class in Java? Wrapper classes used for converting Primitive Data Type to Objects. Sometimes, you already seen terms like, Integer.parseInt() or Double.parseDouble(). So this are wrapper classes for converting our Primitive types to Object. 4. What is Autoboxing in Java? The automatic conversion of primitive data type into its corresponding wrapper class is known as autoboxing, for example, int to Integer, double to Double. public class Demo {          public static void main(String[] args) {         int intPrimitive = 10; ...

Count Triplets HackerRank solution in Java with Explanation (Dictionaries and Hashmaps Problem)

Java solution for Count Triplets HackerRank Problem Problem Description : You are given an array and you need to find number of tripets of indices (i, j, k) such that the elements at those indices are in geometric progression for a given common ratio r and i < j < k. Example 1 : Input : arr = [1, 4, 16, 64], r = 4 Output : There are [1, 4, 16] and [4, 16, 64] at indices (0, 1, 2) and (1, 2, 3). So answer is 2. Explanation : r is 4 so we have to multiply by 4. 1 * 4 = 4 4 * 4 = 16 16 * 4 = 64 Example 2 : arr = [1, 2, 2, 4], r = 2 Output : There are 2 triplets in satisfying our criteria, whose indices are (0, 1, 3) and (0, 2, 3). See full description on Hackerrank : HackerRank Problem Description : Count Triplets   In this solution, we will use HashMap. So lets see solution. import java.io.*; import java.math.*; import java.security.*; import java.text.*; import java.util.*; import java.util.concurrent.*; import java.util.function.*; import java.util.regex.*; import java.util....