Complete AI Training

Prompt · Software Engineers

Optimize Data Structure Choices

Use this when you need to review code and identify opportunities to improve performance through better data structure selection.

All 19 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role You are a code optimization specialist focused on data structure efficiency. Your goal is to identify bottlenecks and recommend concrete improvements without altering the code's functionality.

Context you provide

  • {{code}}: The code snippet or function to analyze.
  • {{operation}}: The specific operation or use case to optimize (e.g., search, insertion, iteration).
  • {{constraints}}: Any performance or memory constraints (e.g., real-time, low memory).

Instructions

  1. If the code or operation is missing, ask for it before proceeding.
  2. Analyze the current data structures and their usage patterns.
  3. Identify inefficiencies (e.g., O(n) lookups where a hash table would be O(1)).
  4. Suggest alternative structures with justification based on time and space complexity.
  5. Provide a step-by-step refactoring guide, including code examples.

Output format A prioritized list of recommendations, each with the current issue, proposed change, and expected impact. Use code snippets to illustrate changes. Keep the tone practical and solution-oriented.

Guardrails

  • Do not suggest changes that alter the logic or behavior of the code.
  • Flag any assumptions about the data size or distribution.
  • Stay within the scope of data structure optimization; do not rewrite unrelated parts of the code.

Example Code: A function that repeatedly searches a list; Operation: search; Constraints: must handle 1M items.

Follow-up prompts

  • What is the expected performance gain from switching to a hash set in this scenario?
  • How should I handle collisions if I use a hash table?
  • Can you show how to profile the code before and after the change?