Coding Questions (Must Practice)
1. Find Duplicate Elements in Array
Logic:
- Use HashSet
- If element already exists → duplicate
Code:
“`java id=”c1″
import java.util.*;
public class Duplicate {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 2, 4, 1};
Set set = new HashSet<>();
for (int num : arr) {
if (!set.add(num)) {
System.out.println("Duplicate: " + num);
}
}
}
}
---
## 2. Find Largest Element in Array
### Logic:
* Compare each element
### Code:
java id=”c2″
public class Largest {
public static void main(String[] args) {
int[] arr = {10, 20, 5, 40};
int max = arr[0];
for (int num : arr) {
if (num > max) {
max = num;
}
}
System.out.println("Largest: " + max);
}
}
---
## 3. Count Frequency of Characters
### Logic:
* Use HashMap
### Code:
java id=”c3″
import java.util.*;
public class Frequency {
public static void main(String[] args) {
String str = “java”;
Map map = new HashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}
System.out.println(map);
}
}
“`
Top Interview Questions (Rapid Revision)
1.What is HashMap?
Answer:
HashMap stores key-value pairs and allows one null key and multiple null values. It is not synchronized.
2.HashMap vs ConcurrentHashMap
Key Difference:
- HashMap is not thread-safe
- ConcurrentHashMap is thread-safe
3.What is Exception Handling?
Answer:
Exception handling is used to handle runtime errors using try-catch blocks.
4.Checked vs Unchecked Exception
Answer:
- Checked → Compile-time (IOException)
- Unchecked → Runtime (NullPointerException)
5.What is Multithreading?
Answer:
Multithreading allows concurrent execution of two or more threads to maximize CPU usage.
6.What is Synchronization?
Answer:
Synchronization is used to control access of multiple threads to shared resources.
7.What is REST API?
Answer:
REST API is used for communication between client and server using HTTP methods.
8.What is Dependency Injection?
Answer:
Providing objects to a class instead of creating inside it.
9.What is Microservices?
Answer:
Architecture where application is divided into small independent services.
10.What is JWT?
Answer:
Token-based authentication mechanism for secure communication.