1756. Design Most Recently Used Queue
Design Most Recently Used Queue (LeetCode 1756) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
How to solve Design Most Recently Used Queue
Design Most Recently Used Queue is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
Work towards the solution in these steps:
- You can store the data in an array and apply each fetch by moving the ith element to the end of the array (i.e, O(n) per operation).
- A better way is to use the square root decomposition technique.
- You can build chunks of size sqrt(n). For each fetch operation, You can search for the chunk which has the ith element and update it (i.e., O(sqrt(n)) per operation), and move this element to an empty chunk at the end.
The reference solution runs in O(sqrt(n)) time and O(n) space.
The Design Most Recently Used Queue solution and editorial walks through the full approach with reference code.
Problem statement
You are given an integer n representing the size of a queue initialized with the integers from 1 to n in increasing order ([1, 2, ..., n]). You need to design a data structure that supports the following operation:
- fetch(k): Removes the k-th element (1-indexed) from the front of the queue, moves it to the end of the queue, and returns its value.
Implement the Most Recently Used Queue that supports initializing with n and performing multiple fetch(k) operations efficiently.
Constraints:
- 1 <= n <= 2000 - 1 <= k <= current queue size for each fetch(k) operation - The number of fetch operations will not exceed 2000
Return the value of the element fetched for each fetch(k) operation.
Examples
Example 1
Input: {"operations": ["MRUQueue", "fetch", "fetch", "fetch"], "parameters": [[5], [3], [3], [3]]}
Output: [null, 3, 4, 5]
Explanation: Initializes queue [1,2,3,4,5], fetches 3rd element (3), moves it to end, then fetches 3rd (4), then 3rd (5).
Example 2
Input: {"operations": ["MRUQueue", "fetch", "fetch"], "parameters": [[2], [2], [1]]}
Output: [null, 2, 1]
Explanation: The queue starts as [1, 2], so fetching position 2 returns 2 and moves it to the end, then fetching position 1 returns 1.
Topics and companies
Topics: Array, Hash Table, Stack, Design, Binary Indexed Tree, Ordered Set.
Reported in interviews at Google.
How to practise Design Most Recently Used Queue on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(sqrt(n)) time and O(n) space.
Open Design Most Recently Used Queue in the code editor, or read the Design Most Recently Used Queue editorial for a worked solution with its approach and complexity analysis.
Where Design Most Recently Used Queue sits in the DSA pattern sheet
Problems related to Design Most Recently Used Queue
Other problems that use the same Design (General/Specific) and Design Patterns techniques:
- All O`one Data Structure — Hard, same subpattern
- Design a Stack With Increment Operation — Medium, same subpattern
- Design a Text Editor — Hard, same subpattern
- Design Circular Deque — Medium, same subpattern
- Design Circular Queue — Medium, same subpattern
- Design Compressed String Iterator — Easy, same subpattern
Design Most Recently Used Queue FAQ
What is the LeetCode number of Design Most Recently Used Queue?
Design Most Recently Used Queue is LeetCode problem 1756, rated Medium.
What pattern is used to solve Design Most Recently Used Queue?
Design Most Recently Used Queue is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
What is the time complexity of Design Most Recently Used Queue?
The reference solution runs in O(sqrt(n)) time and O(n) space.
Which companies ask Design Most Recently Used Queue?
Design Most Recently Used Queue has been reported in interviews at Google.