Warm‑up: Choose the Right Data Structure + Big‑O (with answer)
Warm‑up (with answer): Given operations from an "Order-Search" screen, choose the best data structure and explain time complexity. Operations: 1) Add orderId 2) Check if orderId exists 3) Retrieve most-recent N orderIds quickly 4) Remove an orderId ✅ Warm-up answer covers: HashSet/Dictionary for existence, LinkedList/Queue/Stack for recency, and typical Big‑O reasoning.
using System; // Console
using System.Collections.Generic; // HashSet, LinkedList
namespace ItTechGenie.M1.DSA.WarmUp
{
public static class Warmup
{
// ✅ Warm-up TODO: Student must implement only this method
public static void ChooseDsAndExplain()
{
// TODO:
// - Write short explanation in Console about:
// a) HashSet<string> for Exists / Remove (avg O(1))
// b) LinkedList<string> to keep recency (O(1) add to front, O(1) remove if node known)
// c) Combine with Dictionary<string, LinkedListNode<string>> for O(1) remove in LRU style
throw new NotImplementedException();
}
}
internal class Program
{
static void Main()
{
Warmup.ChooseDsAndExplain(); // prints explanation
}
}
}
Warm‑up Answer (Reasoning + Big‑O)
Best practical combination: - For fast existence & delete: HashSet<string> or Dictionary<string, ...> (average O(1)). - For "most recent N": LinkedList<string> (add to front O(1); take first N by traversal). - For remove by id in O(1): Dictionary<string, LinkedListNode<string>> + LinkedList (classic LRU pattern). Complexities (average): - Add: O(1) - Exists: O(1) - Get recent N: O(N) to read N nodes - Remove: O(1) if you have node reference; otherwise O(N) to search list.
// One valid printed explanation:
Console.WriteLine("Use Dictionary<string, LinkedListNode<string>> + LinkedList<string> (LRU pattern).");
Console.WriteLine("Exists/Remove/Move-to-front are avg O(1), get recent N is O(N).");