Posts

Showing posts with the label Java Interview Questions

Serialization, Deserialization and Externalization in Java

Image
In Java interviews, one of the most common topics you’ll encounter is object persistence —how Java objects are saved, transferred, and reconstructed. This is where concepts like Serialization , Deserialization , and Externalization come into play. These aren’t just theoretical ideas; they are crucial in distributed systems, caching, session management, and inter-process communication . Let’s explore these concepts step by step, highlight their differences, and discuss real-world use cases. 1. What is Serialization in Java? Serialization is the process of converting a Java object into a byte stream so that it can be stored in a file, transmitted over a network, or persisted in a database. How it works: The class must implement the Serializable interface (marker interface). Use ObjectOutputStream.writeObject() to serialize an object. Non-transient, non-static fields are serialized; transient fields are skipped. ...

Cycle Sort in Java — An In-Depth Guide

Image
When preparing for interviews at top companies like Amazon, Google, and Microsoft , you will often encounter problems that require efficient in-place sorting . One such elegant yet lesser-known algorithm is Cycle Sort . Unlike traditional sorting algorithms, Cycle Sort minimizes the number of writes, making it highly efficient when write operations are costly. 🔹 What is Cycle Sort? Cycle Sort is an in-place, comparison-based sorting algorithm. It works by placing each element directly in its correct position using the concept of cycles . Once an element is placed in the right spot, we continue the cycle until the entire array is sorted. Key Features: Minimizes the number of write operations (best choice when memory writes are expensive). Performs sorting in-place with constant space complexity (O(1)). Has a time complexity of O(n²), which makes it less practical for large datasets. 🔹 How Cycle Sort Works Let’s break the algorithm into simple steps: Sta...

ArrayList vs LinkedList in Java

Image
Java’s Collections Framework provides two commonly used List implementations: ArrayList and LinkedList . While both implement the List interface , they differ in their underlying data structures, performance, and use cases. Let’s break down their differences with examples and diagrams. 1. Underlying Structure ArrayList → Backed by a dynamic array. Elements are stored in contiguous memory . LinkedList → Implemented as a doubly-linked list. Each node stores data + pointers to the previous and next node. Diagram: ArrayList (Dynamic Array) [ 0 ] → [ 1 ] → [ 2 ] → [ 3 ] → [ 4 ] (index based access) LinkedList (Doubly Linked Nodes) NULL ← [0 | *] ↔ [1 | *] ↔ [2 | *] ↔ [3 | *] → NULL 2. Code Example ArrayList Example: import java.util.*; public class ArrayListExample { public static void main(String[] args) { List list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("C+...

ConcurrentHashMap vs Synchronized HashMap in Java

Image
Introduction In multi-threaded Java applications, managing shared data safely and efficiently is crucial. Two common approaches are using Synchronized HashMap (via Collections.synchronizedMap(...) ) and ConcurrentHashMap . While both aim to provide thread-safety, their implementations and performance characteristics differ significantly. This post explores these differences in detail. What is a Synchronized HashMap? Created by wrapping a regular HashMap using: Map<K, V> syncMap = Collections.synchronizedMap(new HashMap<>()); Enforces synchronization at the object level , meaning only one thread can access the map at any time (read or write). Supports null keys and values since it relies on HashMap behavior. Iterators are fail-fast , throwing ConcurrentModificationException if the map is modified while iterating. Simpler implementation, suitable when concurrency is low or performance is not cri...

Understanding Marker Interfaces in Java

Image
In Java, a Marker Interface is an interface that does not contain any methods or fields. It serves as a “tag” or “marker” to give special meaning to the classes that implement it. This way, the Java runtime or compiler can identify those classes and provide them with additional capabilities. 🔹 What is a Marker Interface? A marker interface is essentially an empty interface. When a class implements a marker interface, it tells the JVM that the class should be treated in a particular way. // Example of a Marker Interface public interface Serializable { // No methods or fields } public class Student implements Serializable { private int id; private String name; } Here, the Student class implements Serializable . Even though the interface is empty, the JVM knows that objects of Student can be serialized. 🔹 Popular Examples of Marker Interfaces in Java Serializable – Marks classes so their objects...