AI can solve almost any standard DSA problem in seconds — which is precisely the trap. If you let it jump straight to the optimal solution, you skip the reasoning that makes you a better engineer. The right prompt pattern uses AI as a tutor, not a cheat sheet.
Whether you are preparing for interviews or trying to choose between a HashMap and a TreeMap for a real production decision, AI is at its most useful when it walks you through the reasoning. Asking for "the answer" gives you code; asking for "the approach, then the trade-offs, then the code" gives you understanding. This tutorial shows you how to structure both kinds of prompts.
Every algorithmic problem has four layers you can think through: the problem restatement, the approach choices (brute force, optimised, edge cases), the code, and the complexity analysis. A great prompt asks AI to walk through each layer in order — so you can stop and discuss at any point.
Think of the AI as a senior who has solved the problem a thousand times. The wrong question is "what's the answer?" — they would just tell you. The right question is "walk me through how you'd approach this if you'd never seen it." That's the prompt you want to write.
The default prompt is to paste the LeetCode problem and ask for the solution. You get an optimised solution and no understanding.
Weak prompt
solve this in python
Given an array of integers nums and an integer target,
return the indices of two numbers that add up to target.
The AI returns the textbook O(n) HashMap solution. You learn nothing about why it's better than the O(n²) brute force, when the HashMap matters, or what trade-offs exist for sorted vs unsorted input.
Strong prompt
You are a calm DSA tutor. Walk me through this problem in four stops:
Problem:
Given an array of integers nums and an integer target, return the indices
of the two numbers that add up to target. You may assume each input has
exactly one solution and you may not use the same element twice.
Stops:
1. RESTATE
- Restate the problem in your own words.
- List inputs, outputs, and constraints I should clarify with an
interviewer (sorted? duplicates? negative numbers?).
2. APPROACHES (no code yet)
- List 2–3 approaches in order of increasing efficiency.
- For each: rough idea, time complexity, space complexity, when it
would be preferred over the others.
3. CODE
- Implement the BEST approach in Python 3.11.
- Use clear variable names, no one-letter names.
- Add a 1-line comment above any non-obvious line.
4. COMPLEXITY & EDGE CASES
- State final time and space complexity.
- List the edge cases the implementation handles correctly.
- List one realistic input where this approach would be a poor choice
and explain why.
Pause between sections so I can ask questions. Don't move to the next
stop until I say "next".
You now learn the problem rather than collect the answer. You can stop at the approaches stage to confirm your intuition, then proceed. The same prompt works for graph problems, DP problems, tree problems — only the problem statement changes.
Tip: Once you understand a problem, ask the AI to generate three variations of it (e.g. "what if the array is sorted? what if we want all pairs? what if we want pairs that sum to within K of target?"). Solving variations is how you build real algorithmic intuition.
Pick a problem you've already solved on LeetCode or HackerRank. Run the four-stop prompt on it. Does the AI's "approaches" list include the approach you actually used? Did it suggest a better one you missed?
Pick a problem you find intimidating (dynamic programming or graph traversal usually). Stop after each stop and try to predict what the next one will say. Resume only after writing your prediction. This is how you turn watching into learning.
For a real-world data-structure decision in your code (HashMap vs LinkedHashMap, Array vs LinkedList, Set vs SortedSet), ask the AI:
List 3 access patterns where each option wins. Pick the right one for [your specific use case].
This bridges algorithmic theory and production decisions.
Sign in to join the discussion and post comments.
Sign inAdvanced Prompt Engineering Techniques
Master the powerful techniques AI experts use every day. Chain-of-thought, RAG, agents, function calling, prompt evaluation, and much more — 20 deep-dive tutorials.
Prompt Engineering for Business & Productivity
Use AI to work smarter — automate tasks, make better decisions, and communicate professionally. 12 practical business prompt tutorials for professionals.
Prompt Engineering for Specific AI Tools
Tool-by-tool mastery — deep dives into ChatGPT, Claude, Gemini, GitHub Copilot, Midjourney, Stable Diffusion, and more. Learn the exact prompting techniques each platform rewards.
Foundations of Prompt Engineering
The must-know basics of prompt engineering. Learn what prompts are, how AI models read them, and how to write clear instructions that get great results.
Prompt Engineering Projects & Real-World Applications
Twelve hands-on projects that turn prompt engineering theory into a portfolio. Build chatbots, content generators, RAG systems, and more.
Prompt Engineering for Data Science & Analytics
Supercharge your data workflows with AI. 15 practical tutorials on using prompt engineering for data cleaning, EDA, machine learning, SQL, visualisation, and more.