PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers
|Home/Coding & Algorithms/Amazon

Implement Cache and Count Components

Last updated: May 2, 2026

Quick Overview

This question evaluates proficiency in data structure design and management for the LRU cache and in graph connectivity algorithms for counting connected groups, measuring competency in implementing efficient mutable structures and reasoning about component discovery.

  • medium
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Implement Cache and Count Components

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are asked to solve two coding problems. ### Problem 1: Implement an LRU Cache Design a data structure that supports a fixed-capacity least-recently-used cache. Implement the following operations: - `LRUCache(int capacity)`: Initializes the cache with a positive capacity. - `int get(int key)`: Returns the value associated with `key` if it exists; otherwise returns `-1`. Accessing a key makes it the most recently used item. - `void put(int key, int value)`: Inserts or updates the value for `key`. If the cache exceeds its capacity, evict the least recently used key. Both `get` and `put` should run in `O(1)` average time. ### Problem 2: Count Connected Groups in an Undirected Graph You are given an integer `n` representing nodes labeled from `0` to `n - 1`, and a list of undirected edges `edges`, where each edge is `[a, b]`. Return the number of connected groups in the graph. Example: ```text Input: n = 5, edges = [[0,1], [1,2], [3,4]] Output: 2 Explanation: Nodes {0,1,2} form one group, and nodes {3,4} form another. ``` Your solution should handle isolated nodes as separate connected groups.

Quick Answer: This question evaluates proficiency in data structure design and management for the LRU cache and in graph connectivity algorithms for counting connected groups, measuring competency in implementing efficient mutable structures and reasoning about component discovery.

Related Interview Questions

  • Find Valid IP Addresses in Files - Amazon (medium)
  • Implement Optimal Bucket Batching - Amazon (hard)
  • Implement Cache and Rotate Matrix - Amazon (medium)
  • Find Longest Activatable Server Streak - Amazon (hard)
  • Build the Largest Available Sequence - Amazon (medium)
Amazon logo
Amazon
Mar 7, 2026, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0

You are asked to solve two coding problems.

Problem 1: Implement an LRU Cache

Design a data structure that supports a fixed-capacity least-recently-used cache.

Implement the following operations:

  • LRUCache(int capacity) : Initializes the cache with a positive capacity.
  • int get(int key) : Returns the value associated with key if it exists; otherwise returns -1 . Accessing a key makes it the most recently used item.
  • void put(int key, int value) : Inserts or updates the value for key . If the cache exceeds its capacity, evict the least recently used key.

Both get and put should run in O(1) average time.

Problem 2: Count Connected Groups in an Undirected Graph

You are given an integer n representing nodes labeled from 0 to n - 1, and a list of undirected edges edges, where each edge is [a, b].

Return the number of connected groups in the graph.

Example:

Input: n = 5, edges = [[0,1], [1,2], [3,4]]
Output: 2
Explanation: Nodes {0,1,2} form one group, and nodes {3,4} form another.

Your solution should handle isolated nodes as separate connected groups.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Amazon•More Software Engineer•Amazon Software Engineer•Amazon Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.