Problem Solving with Code
Tips and Tricks to Save the World (and your interview)
As I learn more of the technical and syntactical side of coding, I find myself continually drawn to some of the more abstract, hypothetical uses — “given a list of numbers, how would I find the pairs that add up to k?” — as well as those of the philosophical — “how would I solve the Theseus’ Ship paradox through Ruby?” Examining a new problem, it can be a challenging exercise splitting one’s mind between the questions of what [is going on?] and how [will I solve this?], but this bifurcation is both the gift and the curse of a programmer.
The three main steps I’ll discuss are:
- Wishful Coding
- Solving it manually (“brute-forcing” the problem)
- Breaking down the problem (or divide and conquer)
To make it a little bit easier, I offer these amalgamated tips for methodically solving a given code problem:
The World Cup Semifinal Problem (via CodeFights)
Given the above matchups, and ignoring the rules of penalty shoot-outs, how would one determine the potential winners of the World Cup given two-round (combined Semifinal and Final) goal totals of b, f, c, and e (representing Belgium, France, Croatia and England)? I.e., For b = 0, c = 3, e = 1, and f = 2, the output should bepossibleChampions(b, c, e, f) = ["Croatia", "France"].
Wishful Coding
Start at the end — what would the last line of code look like?
Our goal is to return an array of team names, here represented by the variableposs_champs. Anchor yourself against the ultimate objective — what am I solving? (Returning the name of the team who could win given their total number of goals scored in the semi-final and final rounds)
The logic, the meat of the answer, then becomes the follow-up to the answer — how do I solve it?
We want some logic to occur for each team, where if that logic occurs the name of that team is added to the solution array.
Get Noah Berman’s stories in your inbox
Join Medium for free to get updates from this writer.
Using wishful coding, we already have the basic framework in place. By working backwards, we have a destination in place, and now can look for a path to get there.
Solve it manually (brute force)
We need to really understand our potential solution before we can create the abstract logic to define it in all cases. Perhaps the easiest way to achieve this is by coming up with working examples of the code being true.
Using the above example (b = 0, c = 3, e = 1, and f = 2) , why does possibleChampions(b, c, e, f) = ["Croatia", "France"] ?
- France plays Belgium first, thus France could score 1 goal against Belgium’s 0 in the Semifinal, and then score 1 goal against Croatia’s 0 in the Final (assuming Croatia scored all 3 goals against England in the semifinal)
- Croatia plays England first, thus Croatia could score 2 goals to beat England’s 1 in the Semifinal, and then score 1 goal in the Final against France’s 0 (assuming France scored 2 goals against Belgium in the semifinal)
Break it down
Now that we understand what’s going on, let’s take it apart piece-by-piece (divide and conquer). Our logical constraints are thus:
- Belgium plays France and England plays Croatia in the Semifinal round. Thus, each team must have more goals than their Semifinal opponent.
- Each team must then have more goals than their opponent in the Final round. Since
b,f,c, andeeach represent the combined Semifinal and Final goal totals, it’s possible for a team to score all of their goals in the Semifinal round. If we are looking to return all possible winners, then, any given country really only needs to score more than 1 (to score at least one goal in the Final.) - Furthermore, for a team to win the Final, they must have at least one goal leftover from their total, deducting their Semifinal opponent’s goal total plus one (in order to beat them). Therefore, each possible champion must score at least two more than their semifinal opponent.
We can represent all three of these through code:
Put it all together and we have some working code!
Conclusion
While this problem wasn’t the most complicated you’ll face, it does illustrate the basic framework we can use to solve any issue. The trick is to stay disciplined with your thinking, and approach problems starting with the solution and working backwards.
Practice
Given a list of numbers (can you find all the pairs of numbers whose sum equals k?
(answer)
Resources:







