Github Copilot · Free Practice Question Medium

Question 12

You are using GitHub Copilot to generate a Python function that takes a list of integers and a target sum, returning two numbers that add up to the target. The function should be efficient and handle edge cases like no valid pair. Which prompt is best?

  • A

    "Create Python code that implements an algorithm for finding a pair of numbers in a list that sum to a target value."

  • B

    "Write a Python function that returns two integers from a list that add up to a target sum."

  • C

    "Generate Python code for a function that finds two integers in a list that sum to a target, and handle edge cases like no pair found. Prioritize an efficient approach."

  • D

    "Create a Python function that accepts a list and a target sum and returns two numbers from the list that add up to the target sum. If no such pair exists, return None."

Reveal correct answer

Correct answer: C

A.

Although this prompt asks for an algorithm, it lacks the specificity of handling edge cases and efficiency. The AI might generate a correct but inefficient solution (e.g., brute force) or not account for situations where no pair is found.

B.

While this prompt is clear, it lacks specificity in handling edge cases (e.g., no valid pair) and performance requirements (e.g., prioritizing efficiency). Copilot may still produce a correct function, but it might not handle all cases, leading to an incomplete solution.

C.

This prompt is specific, detailed, and directs GitHub Copilot to both prioritize efficiency and handle edge cases (such as no pair found). It gives the AI sufficient information to focus on performance (e.g., avoiding a brute-force solution) while ensuring correctness. This prompt is the most refined in terms of prompt engineering, as it clearly outlines the task and performance requirements.

D.

This prompt improves upon A by specifying the need to return None if no valid pair exists, but it still lacks the instruction to prioritize efficiency. Without that, the AI might generate a solution that works but is not optimal (e.g., a brute-force O(n^2) approach).

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need