NFT Queue Processing - codepath/compsci_guides GitHub Wiki
Unit 4 Session 1 Standard (Click for link to problem statements)
U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
-
Q: What is the structure of the input?
- A: The input is a list of dictionaries, where each dictionary contains an NFT's name and its processing time.
-
Q: What is the output?
- A: The output is a list of NFT names in the order they were processed, following a First-In, First-Out (FIFO) sequence.
-
Q: How should the function handle the order of NFTs?
- A: The function should process NFTs in the order they appear in the list, regardless of their processing time.
-
Q: Are there any constraints on the input list?
- A: The problem does not specify constraints, so we assume the list can be of any length.
P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a queue to process NFTs in the order they were added. Dequeue each NFT and add its name to the final processing order.
1) Initialize an empty list called `processing_order` to store the names of NFTs in the order they are processed.
2) Convert `nft_queue` into a deque to facilitate FIFO operations.
3) While the `queue` is not empty:
a) Dequeue the first NFT from the queue.
b) Append the NFT's name to `processing_order`.
4) Return the `processing_order` list.
**⚠️ Common Mistakes**
- Forgetting to use the correct data structure (queue) to maintain the FIFO order.
- Misunderstanding the role of processing time in determining the order (processing time does not affect the order in this problem).
I-mplement
from collections import deque
def process_nft_queue(nft_queue):
processing_order = []
queue = deque(nft_queue) # Initialize the queue
while queue:
nft = queue.popleft() # Dequeue the first NFT
processing_order.append(nft["name"])
return processing_order