# Pro ChatGPT Prompt for LeetCode

As an avid LeetCode user, I’ve found that ChatGPT can be a powerful ally in tackling coding interview prep. However, like any tool, you need to know how to use it properly. In this post, I’ll share my top tips for crafting effective ChatGPT prompts that help you solve LeetCode problems efficiently.

## Understand the Problem First

Before jumping into asking ChatGPT for the solution, always take the time to thoroughly understand the problem yourself first. Read through the problem description, inputs, outputs, test cases, and any other details. Summarize the key objectives and constraints in your own words.

This helps you formulate more targeted prompt questions for ChatGPT. You can even bold the actual problem statement to make it clear in your prompt. Here’s an example prompt structure:

``````**Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid**. An input string is valid if:

1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.

Could you provide Python code that efficiently solves this problem? The key aspects are...``````

Spending time to comprehend and clearly state the problem pays dividends in getting better solutions from ChatGPT.

## Specify Language and Data Structures

Be explicit about requiring code in a particular language like Python or Java. Also call out if you need the solution to use specific data structures like arrays, stacks, queues, hash tables etc.

ChatGPT sometimes makes suboptimal choices if you don’t guide it. For example:

``Please provide a Java solution for this problem using a Stack data structure. The key aspects are...  ``

Specifying languages and data structures aligns ChatGPT better to LeetCode expectations.

## Provide Guidance on Efficiency

In coding interviews, the time and space complexity of your solution matters greatly. Prompt ChatGPT to optimize for efficiency by calling out requirements like:

``Aim for O(n) time complexity and O(1) space complexity if possible. The key aspects are...``

You can also ask ChatGPT to explain its approach for achieving the desired efficiency. This helps you learn techniques for writing performant code.

LeetCode problems often have multiple valid solutions with different time and space trade-offs. Ask ChatGPT to provide multiple approaches like:

``Could you please provide two different solutions for this problem - one optimizing for time complexity, the other optimizing for space complexity. Use clear comments explaining each one.``

Evaluating different solutions from ChatGPT can reveal alternative ways of tackling problems that you may not have considered.

Well-commented code is tremendously helpful for understanding the logic and flow. Always prompt ChatGPT to use clear, descriptive comments like:

``Use clear comments explaining each section and key aspects like time and space complexity analysis.``

Code without comments is very opaque, so ensure ChatGPT elucidates its working properly.

## Verify Through Examples

While ChatGPT generates sensible seeming solutions most of the time, there can be logical gaps or bugs. Rigorously verify the code with examples covering different edge cases and inputs.

You can even provide sample inputs and expected outputs in the prompt e.g.:

``````Input: s = "()"
Output: true

Input: s = "()[]{}"
Output: true

Could you provide code that passes these test cases?``````

Or ask ChatGPT itself to provide examples covering edge cases.

Careful validation prevents faulty solutions from going into your actual LeetCode submissions.

## Fix Issues Collaboratively

Despite best efforts, you may sometimes find issues with ChatGPT’s initial solutions. Instead of discarding it outright, take a collaborative approach – explain the bug and ask ChatGPT to fix it e.g.:

``````I tested your code and found it fails for this input: s = "(]"
This opens a bracket that isn't closed properly.

Could you update the code to handle this edge case and explain the fix?``````

More often than not, ChatGPT will respond with an updated, working solution. This helps improve solutions faster.

The key is prompting ChatGPT to learn from mistakes and strengthen its code.

## Practice Makes Perfect

Like anything, crafting effective ChatGPT prompts is a skill that improves with practice. Experiment with different prompt formulations and see what works best for you.

Pay attention to prompt patterns that produce better solutions and iterate on those. Learnings from each problem will lead to improvements over time.

Be patient, persistent and keep working problems with ChatGPT. Follow these tips and you’ll be able to leverage AI for better LeetCode practice!