If Programming Tutorials Were Like Dance Instructors
Maybe the real Udemy course is the friends we made along the way
(A parody of nothing in particular)
The purpose of coding is not to create, or to build out new functionality, or to make money — these things are all secondary. Coding is about having fun. If you are in the process of preparing for a coding interview right now, and are not having at least a little fun, then something is seriously wrong.
In this tutorial, we will be doing a very basic coding exercise called Buy And Sell Stock. The technique we will use is Sliding Window, but remember: There is no wrong way to code. As long as you type to a beat, learn new things, and enjoy yourself, we will absolutely hire you instead of making you feel terrible about your abilities.
Our Process
At CORGICorporation, we believe in making every coding interview an unforgettable experience. We start by taking candidates to our open bar, so they can loosen up, but we understand that not everyone is comfortable drinking. Because of this, we next take candidates to visit our chocolate fountain, kombucha machine (yes, you read that correctly), and snack bar before offering them a scenic view of Los Angeles from our downtown office. Only when we are sure they are comfortable do we begin the interview.
Warm-Up
Description:
You are given an array prices
where prices[i]
is the price of a given stock on the ith
day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0
.
After reading the problem, I always start with some warm-ups. To remind myself that coding should be fun and nothing has to compile, I write down a few lines of gibberish.
This, of course, wouldn’t compile so I comment it out.
Next in my warm-up, I make a pointless hashmap and fill it with the same keys and values for no reason. I make a vector with the value 777 and then return 0 for no reason.
This, of course, would be inefficient so I comment that out as well.
First Pass
Now is the time to just try something out. I code thoughtfully but effortlessly, just to enjoy the process of going through the motions. I know we can do this in O(n²) with a nested for loop, so long as the time we sell a stock is after we purchase it.
I write some code, I submit it, it fails, and that’s okay. There are no bad solutions, only happy accidents.
No one has ever failed an interview because they submitted a brute-force solution and struggled with finding ways to optimize it (note to self: citation needed).
The Sliding Window Technique
So, for this technique, the basic steps are
- Start windowStart and windowEnd at index 0
- Slide your window right as long as you are still making a gain from “buy price” to “sell price”
- Start a new window if at any point you obtain a negative return
I know. That’s a lot. Let me show you
But the truth is, you can miss a step or two and no one will care that you haven’t gotten it perfectly. As long as you get your code mostly right, no one will penalize you. Coding is about rhythm, not precision. It’s not like we’re in a profession where the slightest mistake will give you a radically wrong answer with bugs that take crucial minutes to solve in an interview, and hours to solve in the actual field.
No. Coding is effortless and easy because it is so forgiving.