This one is probably the “Hello World” of algorithms. Meaning, it’s the easiest. The spec asks us to write a function that returns the sum of two numbers.
//Example
// For param1 = 1 and param2 = 2, the output should be add(param1, param2) = 3.
// Input and Output
// Input => integer for param1 and param2 with guaranteed constraints of -1000 <= param <= 1000
// Output => integer => Sum of the two inputs
Okay so this seems like a simple Elementary problem. I did this in Python because Python is the best language. Go ahead and prove me wrong. I dare you.
def add(param1, param2):
// We want to get the sum of the two inputs so let's just add them together
return param1 + param2
Boom. That’s it. Main thing with these code challenges, whether it’s Add or Median of Two Sorted Arrays , don’t over think it. A lot of times we get so stressed out with “Oh my God, how am I going to solve this?? I need help!!” That is where the UPER framework comes into play.
U- Understand
What does the spec want me to do? What are my constraints? Any edge cases I should consider?
P- Plan
This is where diagramming or psuedocode comes in. In the code block above I used comments to help wrap my head around what I am doing and plan out my next steps.
E- Execute
Yay!! This is where we actually start coding and implementing our plan.
R- Reflect
Was our code optimal? What could I have done better?
The UPER framework is not linear. You should definitely go from U-E but once you hit R you might want to go back up to P and plan how to make your code better or more optimal. Then E again and R again.
This was DRILLED into my head by one of my lovely instructors at Lambda School and I’m a better coder for it.