HCL Interview Experience: LRU Cache Coding Question in Java

Last week, I attended an interview at HCL, where I was asked a popular Data Structures and Algorithms (DSA) programming question: Design and Implement an LRU Cache in Java.

This problem tests your understanding of HashMap, Doubly Linked List, Data Structures, and Time Complexity Optimization. It is a useful question for Java Developer interviews.

Problem Statement

Design and implement a Least Recently Used (LRU) Cache with a fixed capacity.

The cache should support the following operations:

  • put(int key, int value) — Adds or updates a key-value pair. If the cache is full, remove the least recently used entry before inserting a new key.
  • get(int key) — Returns the value associated with the key if it exists; otherwise, returns -1.

Whenever a key is accessed or updated, it should become the most recently used entry.

Requirements

  • Use a HashMap for O(1) average-time key lookup.
  • Use a Doubly Linked List to maintain the usage order.
  • Both get() and put() should run in O(1) average time.
  • Keys and values are integers.

Java Solution

The following implementation uses a HashMap and a custom Doubly Linked List with dummy head and tail nodes.

import java.util.HashMap;
import java.util.Map;

class LRUCache {

    private class Node {
        int key;
        int value;
        Node prev;
        Node next;

        Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    private final int capacity;
    private final Map<Integer, Node> cache;
    private final Node head;
    private final Node tail;

    public LRUCache(int capacity) {
        if (capacity <= 0) {
            throw new IllegalArgumentException(
                "Capacity must be positive"
            );
        }

        this.capacity = capacity;
        this.cache = new HashMap<>();

        head = new Node(0, 0);
        tail = new Node(0, 0);

        head.next = tail;
        tail.prev = head;
    }

    public int get(int key) {
        Node node = cache.get(key);

        if (node == null) {
            return -1;
        }

        moveToFront(node);
        return node.value;
    }

    public void put(int key, int value) {
        Node node = cache.get(key);

        if (node != null) {
            node.value = value;
            moveToFront(node);
            return;
        }

        if (cache.size() == capacity) {
            Node lru = tail.prev;
            removeNode(lru);
            cache.remove(lru.key);
        }

        Node newNode = new Node(key, value);
        cache.put(key, newNode);
        addToFront(newNode);
    }

    private void moveToFront(Node node) {
        removeNode(node);
        addToFront(node);
    }

    private void addToFront(Node node) {
        node.next = head.next;
        node.prev = head;

        head.next.prev = node;
        head.next = node;
    }

    private void removeNode(Node node) {
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }
    
    public static void main(String[] args) {
        LRUCache cache = new LRUCache(2);

        cache.put(1, 10);
        cache.put(2, 20);

        System.out.println(cache.get(1)); // 10

        cache.put(3, 30); // Evicts key 2

        System.out.println(cache.get(2)); // -1
        System.out.println(cache.get(3)); // 30

        cache.put(4, 40); // Evicts key 1

        System.out.println(cache.get(1)); // -1
        System.out.println(cache.get(4)); // 40
    }
}

How Does the LRU Cache Work?

The Doubly Linked List maintains entries in usage order:

  • Head: The next node after the dummy head is the most recently used entry.
  • Tail: The node before the dummy tail is the least recently used entry.
  • HashMap: Maps each key to its corresponding node, allowing direct access.

When a key is accessed, its node moves to the front. When the cache is full, the node immediately before the tail is removed.

Example Walkthrough

Assume the cache capacity is 2.

Operation Result Cache Order (MRU → LRU)
put(1, 10)Inserted1
put(2, 20)Inserted2 → 1
get(1)Returns 101 → 2
put(3, 30)Key 2 evicted3 → 1
get(2)Returns -13 → 1
put(4, 40)Key 1 evicted4 → 3

Time and Space Complexity

  • get(key): O(1) average time
  • put(key, value): O(1) average time
  • Space Complexity: O(capacity)

The HashMap provides average O(1) lookup, while the Doubly Linked List supports O(1) insertion and removal when the node is already known.

Key Interview Takeaways

  1. Understand why a HashMap alone cannot efficiently maintain LRU order.
  2. Know how a Doubly Linked List enables O(1) removal of any known node.
  3. Handle existing keys correctly in the put() method.
  4. Remember to update usage order in the get() method.
  5. Use dummy head and tail nodes to simplify insertion and removal logic.

Conclusion

The LRU Cache is a common Java interview problem that combines two fundamental data structures to achieve efficient performance. Practising this question helps strengthen problem-solving skills and prepares you for backend developer interviews.

Have you been asked an LRU Cache question in an interview? Share your experience in the comments!


Tags: HCL Interview Experience, Java Interview Questions, LRU Cache Java, DSA Problems, HashMap, Doubly Linked List, Java Developer Interview Preparation