This community-built FAQ covers the “Pair Sum: Optimized” exercise from the lesson “Technical Interview Problems in Python: Lists”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Technical Interview Practice: Python
FAQs on the exercise Pair Sum: Optimized
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Need broader help or resources? Head here.
Looking for motivation to keep learning? Join our wider discussions.
Learn more about how to use this guide.
Found a bug? Report it!
Have a question about your account or billing? Reach out to our customer support team!
None of the above? Find out where to ask other questions here!
Hello, all my 4 test results are True, but I am not able to pass this lesson getting following message>>
“Your code returned: [0, 4] but should have returned [1, 3]” without any knowledge of arguments used in the function. My solution does not include a dictionary as mentioned in the Instructions, I do not see any bug from my poor point of view.
Thanks in advance for your feedback,
Frantisek
That test is the same one you’ve got at line 19
I believe codecademy’s test is wrong. The pair [0, 4]
comes before [1, 3]
. They are very vague about what “first pair” means, however, since they say “the first” they are certainly saying that there is a single first such pair, and at the very least the tests for the previous one contradicts the tests for this one.
Your solution, while producing correct answers, is incorrect, it runs in O(N2) time but should run in O(N) time (your code is suitable for the previous exercise)
Watch out when making a O(N) solution, it’s easy to make solutions that fail on these:
[1,1,2,1,1,1], 3
should result in 0, 2
[0,2,1,1,1,3], 3
should result in 0, 5
Thanks for your feedback. I reduced the code to 1 iteration only, passing all tests + 2 examples of yours, but still getting the same refusal message…
You’ll always get that error message because the tests are wrong
Your code is still O(N*N), in your loop you can only use operations that take a constant amount of time
An example of a big input where your code will fail to complete in reasonable amount of time:
pair_sum([0] * 100000 + [1,2] + [0] * 100000, 3)
Which should return [100000, 100001]
in less than a second.
My implementation takes 0.08 seconds on my laptop including starting python itself and creating the input list (timing the whole program, not just the function call)
Couldn’t this be done with only a single dictionary?
def pair_sum(nums, target):
complements = {}
for i in range(len(nums)):
if complements.get(target - nums[i]) != None:
return [complements.get(target - nums[i]), i]
else:
complements[nums[i]] = i