00:00

Problem Statement

Input/Output Examples

Visual Explanation

With a doubly linked list, moving a node to the end and removing the front node are both constant-time pointer changes, and the hash map finds any node directly.

Approaches

Both approaches give O(1) operations. One uses the JDK's LinkedHashMap; the other builds the structure by hand.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Every operation is a hash lookup plus a constant number of pointer changes, so get and put are O(1). Memory is proportional to the capacity.

Interview Tips and Recommended Approach

  • Count reads as uses. A get must move the key to the most recent position.
  • Use sentinel nodes. They remove all the special cases for empty lists and end nodes.
  • Know the LinkedHashMap shortcut and say why the manual version is still asked.
  • Discuss thread safety. Neither version is thread-safe; wrap it or use a concurrent cache library.

Recommended Approach: LinkedHashMap in Access Order

Why: it is the idiomatic Java solution, a few lines long, with the same O(1) guarantees as the hand-written structure.