Google Interview Answered in Python


I’m in the process of moving my mindset from static linear scripting to an object oriented one.  My goal is to solve problems with algorithms I create myself.  It’s a tough climb because I come from Systems Admin/Networking side of things and didn’t benefit from Computer Science theory and advanced mathematics.  I’m hoping to eventually move into a DevOps role where I can organically absorb from devs.  It would be great to be able to observe other approaches to problem solving.

The video above is a Google interview prep question from Youtube.  The interviewee solves it with C++, I provide my version in Python below.  Essentially, they are solved in a similar way.  Granted, it took me two weeks of stewing on it and the man above did it on the spot, but he’s a Software Engineer, so I feel pretty good!

The problem is to find pairs of integers that add to 8 from a list assumed to be in ascending order.  Negatives and doubles are possible.


Let’s execute!

[matt@mattcom1 google-interview]$ ./

Found Match: 0 , 8


Found Match: 1 , 7


Found Match: 2 , 6


Found Match: 3 , 5


Final tally of pairs that equal eight: 4

[matt@mattcom1 google-interview]$

I solved this by running a ‘while’ loop on the list in question.  Every iteration of the ‘while’ loop ‘pop( )s’ off the trailing element of the list to compare in the ‘for’ loop against the remaining elements.  When it finds a match equaling eight it removes the compliment and stops the ‘for’ loop(so it won’t count duplicate integers).  This happens over and over until there is one or none elements left in the list.

The function can search for other pairs that add up to any number as well.  It’s set to “8” and “example_list_two” by default.  I hope you enjoyed this post!  Thank you for reading.

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment