Implement LRU Cache with streaming input

Last updated: January 8, 2026

Quick Overview

Implement a Stack data structure that supports push, pop, and peek operations in O(n) time.

Palo Alto Networks
Coding & Algorithms
Machine Learning Engineer
Palo Alto Networks
January 8, 2026
Machine Learning Engineer
Onsite
Coding & Algorithms
Medium

3

11

4,733 solved


Implement a Stack data structure that supports push, pop, and peek operations in O(n) time.

This coding problem is frequently asked during Onsite at Palo Alto Networks. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Palo Alto Networks expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Data structure selection and trade-offs
Graph algorithms and traversal
Time and space complexity analysis
Edge cases and input validation
Hash maps and frequency counting
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you modify your solution to handle streaming input?
  • What happens if the input contains duplicates?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • Can you optimize the space complexity of your solution?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires implementing a Least Recently Used (LRU) Cache, which inherently involves managing a collection of items in a way that allows for efficient retrieval and eviction of the least rec...

Approach
  1. Data Structure Definition: Define a Node class to represent each entry in the cache, with attributes for the key, value, and pointers to the previous and next nodes.
  2. Cache Definition: ...

Submit Your Answer
Markdown supported

Related Questions