Skip to main content

Coding - Mostly Asked Coding Interview Question

 1. Print Mostly occurred string in Hello String

import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class MostOccuringCharacter {
// print most occuring character in hello string and write program to use optional
public static void main(String[] args) {
String str = "Hello";
Optional<Map.Entry<Character, Long>> character = str.chars()
.mapToObj(x -> (char) x)
.collect(Collectors.groupingBy(x -> x, Collectors.counting()))
.entrySet()
.stream()
.max(Map.Entry.comparingByValue());


if(character.isPresent()){
System.out.println("Most occurring character: " +character.get().getKey());
}
//or
character.ifPresent(c -> System.out.println("Most occurring character: " + c.getKey()));
    }
}

2. Sort 1 and 0 from array

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SortOneZeroFromArray {

public static void main(String[] args) {
usingArray();
usingArrayList();
}

private static void usingArrayList() {
int[] arr = {0, 1, 1, 1, 0, 0, 0};
List arrayList = new ArrayList<>();
for (int i = 0; i <= arr.length - 1; i++) {
if (arr[i] == 0) {
arrayList.add(0, arr[i]);
} else {
arrayList.add(arr[i]);
}
}
arrayList.forEach(System.out::println);
}

private static void usingArray() {
int[] arr = {0, 1, 1, 1, 0, 0, 0};
int size = arr.length;
int[] newarr = new int[size];
int startIndex = 0;
int endIndex = size - 1;
for (int i = 0; i <= size - 1; i++) {
if (arr[i] == 0) {
newarr[startIndex] = arr[i];
startIndex++;
} else {
newarr[endIndex] = arr[i];
endIndex--;
}
}
Arrays.stream(newarr).forEach(System.out::println);
}
}

Comments

Popular posts from this blog

Interview questions - Strings

1. Why Strings are immutable?  In Java, String is immutable , meaning once a String object is created, its value cannot be changed . Any operation that seems to modify a String actually creates a new object instead of modifying the existing one. Lets understand with below program.  find code here The String Constant Pool (also known as the String Intern Pool ) is a special memory area inside the heap where Java stores string literals to optimize memory usage and improve performance. When we create String password = "password@123"; using literal it will create instance in String constant pool and when we assign password = "changepassword", it will not change previous instance, but create new one inside String constant pool. Check hash code before and after changing value both are different. Lets understand scenario2 : code public class StringMemoryCheck {     public static void main(String[] args) {         String s1 = "Java";  // String...

Internal Working of HashMap in Java

  A HashMap is a part of Java's Collections Framework , and it stores data in key-value pairs. Internally, it uses a hash table to store entries, making it efficient for fast lookups, insertions, and deletions. The underlying structure of a HashMap is based on a dynamic array of buckets (or "bins") and the hashing technique for quick access. Here’s an overview of how HashMap works internally: 1. Structure of HashMap A HashMap consists of the following key components: Buckets : A bucket is essentially an index in an array where hash collisions are stored. A HashMap is backed by an array, and each entry (a key-value pair) is stored in one of the array's indices (buckets). Entries (Key-Value Pairs) : Each entry is a key-value pair, stored in the buckets. Hashing : A hash function is used to determine the index (bucket) where a key-value pair will be stored. 2. Hashing Process When a key-value pair is added to a HashMap , the key undergoes a hashing process to de...