Coding
AI Prompt for Implementing Data Structures
Implement any data structure from scratch with this AI prompt: clean annotated code, invariant explanations, complexity notes, and thorough tests.
The prompt
Role: You are a computer science instructor who implements data structures with exceptional clarity. Context: Structure: [STRUCTURE_NAME]. Language: [LANGUAGE]. Required operations: [OPERATIONS]. My level: [YOUR_LEVEL]. Task: 1) Explain the core idea in one paragraph: how the structure achieves its guarantees. 2) Implement it in clean, well-commented [LANGUAGE] — production quality, not pseudocode. 3) Annotate the invariants (what must always be true) and where the code maintains them. 4) Analyze each operation's complexity with brief reasoning. 5) Write tests: normal operations, edge cases (empty, full, single element), and invariant checks. Format: Core idea first, then the implementation, then invariants called out, then complexity, then tests. Ask the AI to compare your implementation against the standard library's version, explaining the design trade-offs each made. Libraries optimize for generality; understanding the difference teaches you when to deviate. Constraints: Real working code — it must compile and pass the tests. Explain at a [YOUR_LEVEL] level. Prefer clarity over micro-optimization in the implementation.
Variables to fill in
Replace each [variable] in the prompt with your own details:
[STRUCTURE_NAME] [LANGUAGE] [OPERATIONS] [YOUR_LEVEL]
Example input
STRUCTURE=LRU cache, LANGUAGE=Java, OPERATIONS=get/put in O(1), CAPACITY=1000, LEVEL=intermediate
How to use this prompt
Name the exact structure and the operations it must support — vague requests get vague implementations. Ask for the invariant explanation; it is the learning payoff.
Tips for the best results
- Study the invariants — they are the deep understanding, not the code.
- Run the tests, then try to break an invariant to see what fails.
- Ask how the standard library implements it differently and why.
Frequently asked questions
Why implement instead of using a library?
It implements from scratch with invariant explanations and tests — the best way to truly understand a structure.
Should my implementation match the textbook exactly?
Match the invariants, adapt the details. The prompt prioritizes clarity for learning — production versions add optimizations to layer on later.