Shooonya

Technology | Books | Photography

01 Jul 2020

Cracking the Coding Interview with Deliberate Practice

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

One of the first things you should do in interviews is ask clarifying questions. The first question would be, are you a water management company or a software company? Once the futility has been confirmed, you will ponder about the algorithm. Should I sort? Should I use a stack? Should I use a Priority Queue? Should I jump out of the window?

Many candidates preparing for interviews use some site like leetcode or hackerrank to solve questions like – given a string s of ‘(‘ , ‘)’ and lowercase English characters remove the minimum number of parentheses ( ‘(‘ or ‘)’, in any positions ) so that the resulting parentheses string is valid and return any valid string.

Mugging these questions is not going to help is because short term memory is limited. Try remembering the phone numbers of 10 people. Instead, a better way to learn is with deliberate practice and by building better mental models. These two ideas are so fundamental to learning effectively and have helped many people perform amazing feats in their careers. In the book Peak: Secrets from the New Science of Expertise, Anders Ericsson and Robert Pool reveals this fundamentally new way of thinking about the human potential which you too can adopt to be successful.

Deliberate practice has several characteristics that set it apart from “naive practice,” which is just doing something repeatedly, expecting that the repetition alone will improve one’s performance. Deliberate practice pushes you out of your comfort zone. It also involves feedback. This could come from a teacher or coach or it could come from your experience to monitor and improve. For example, in the trapping the rainwater, if you did not use a two-pointer solution, but a Priority Queue, then you would use that observation as feedback to analyze and improve

Some people flunk a lot of coding interviews. After the interview, the question looks simple and often you wonder why you did not come up with the solution. This is not because you are not talented. It is because you did not practice the right way. To see how to learn with deliberate practice, we have look at chess masters. They develop skills by spending countless hours studying games played by the masters. You analyze a position in depth, predicting the next move, and if you get it wrong, you go back and figure out what you missed. Research has shown that the amount of time spent in this sort of analysis—not the amount of time spent playing chess with others—is the single most important predictor of a chess player’s ability.

What deliberate practice does is create an efficient mental model. The best way to explain this is by using the example of chess players again. Any chess player with some experience does not see the individual pieces. Instead due to years of practice, they see patterns. A master who examines a chessboard sees a collection of chunks that are interacting with other chunks in still other patterns. The ability to recognize and remember meaningful patterns is what separates ordinary players from the extraordinary.

Their years of practice have changed the neural circuitry to produce highly specialized mental representations, which in turn make possible the incredible memory, pattern recognition, problem-solving abilities. As you practice deliberately and learn something new, the model gets updated. This helps in quickly spotting mistakes and correcting them. The more skilled you are, the better your mental representation. Better the mental representation, the more skilled you are.

Through practice, you need to develop sophisticated mental models to find out the accurate technique quite fast. Like a good quarterback, you need to analyze the field and decide whom to pass the ball to. A doctor hearing looking at various aspects of a patient’s parameters has to recall medical knowledge, use the data he has, and make the diagnosis. For this to happen, you should have a way to organize information in a superior way. Coming to coding interviews, you need to slow down and develop efficient mental representations for each of the problems.

Once we understand the adaptability of the brain in this way, human potential can be thought in an entirely different light. This points us to an entirely different approach to learning. With this technique, you will dangerously and scandalously fall in love with learning. There is no point in sitting and solving 200 problems without understanding it. Instead, it is better to do 50 problems, but with deliberate practice which enhances your mental models. By storing these mental models in long term memory, it becomes easier to recall. When you see the trapping rainwater problem the next time, you should be able to use two pointers and solve it with O(n) time complexity.