CodingSaves ~30 minutes

Implement a binary search algorithm

Get a correct, edge-case-proven binary search implementation with a thorough explanation and tests.

The prompt

You are a computer science instructor teaching algorithms. Implement binary search in [LANGUAGE] with the following requirements: iterative implementation (to avoid stack overflow on large inputs), a recursive implementation for comparison, correct handling of edge cases (empty array, single element, target not present, all identical elements, even and odd length arrays), generic type support so it works on any comparable type, clear variable names (avoid low/mid/high — rename to something more descriptive), full test suite with parameterized tests covering all edge cases, and a complexity analysis (time and space, best/average/worst case). Explain why off-by-one errors happen in binary search and how your implementation avoids them.

Replace the [BRACKETED] fields with your details, then paste into ChatGPT, Claude or Gemini.

Want AI to fill this in for you?

Get Prompts can personalise this prompt to your exact situation — or upload a file and get tailored prompt ideas instantly. 3 free edits, no sign-up.

Try it free →

More coding prompts