Skip to content
  • Courses
  • Resources
  • Blog
  • About
Menu
  • Courses
  • Resources
  • Blog
  • About
STUDENT LOGIN
  • Courses
  • Resources
  • Blog
  • About
  • Student Login
Menu
  • Courses
  • Resources
  • Blog
  • About
  • Student Login

Master dynamic programming with the FAST method

  • August 14, 2017

Dynamic Programming with the FAST method

If there’s one topic that my readers seem to struggle with across the board, it’s dynamic programming.

I get it. Dynamic programming seems mysterious and inscrutable. It especially doesn’t help when you go on Leetcode to look at the solutions and someone has come up with this beautiful code that you could never replicate on your own.

The thing is, though, that dynamic programming doesn’t have to be a complete enigma. If you have the right, structured approach you can find the solution to any dynamic programming problem without breaking a sweat.

This is why I wrote Dynamic Programming for Interviews. This ebook not only introduces the FAST method for solving any dynamic programming interview question, but it goes through 5 different examples in detail to show you exactly how to apply the fast method.

This article is an excerpt from the book describing how the FAST method of solving dynamic programming problems works. After reading this article, make sure that you download the ebook HERE to learn by example how you can apply the FAST method during your interviews.


The FAST method

The most successful interviewees are those who have developed a repeatable strategy to succeed. This is especially true for dynamic programming. This is the reason for the development of the FAST method.

There are four steps in the FAST method:

  1. First solution
  2. Analyze the first solution
  3. Identify the Subproblems
  4. Turn the solution around

By following these four steps, it is easy to come up with an optimal dynamic solution for almost any problem.

First solution

This is an important step for any interview question but is particularly important for dynamic programming. This step finds the first possible solution. This solution will be brute force and recursive. The goal is to solve the problem without concern for efficiency. It means that if you need to find the biggest/smallest/longest/shortest something, you should write code that goes through every possibility and then compares them all to find the best one.

Finding the initial brute force solution is an important step for any interview question but is particularly important for dynamic programming. Click To Tweet

Your solution must also meet these restrictions:

  • The recursive calls must be self-contained. That means no global variables.
  • You cannot do tail recursion. Your solution must compute the results to each subproblem and then combine them afterwards.
  • Do not pass in unnecessary variables. Eg. If you can count the depth of your recursion as you return, don’t pass a count variable into your recursive function.

Once you’ve gone through a couple problems, you will likely see how this solution looks almost the same every time.

Analyze the first solution

In this step, we will analyze the first solution that you came up with. This involves determining the time and space complexity of your first solution and asking whether there is obvious room for improvement.
As part of the analytical process, we need to ask whether the first solution fits our rules for problems with dynamic solutions:

  • Does it have an optimal substructure? Since our solution’s recursive, then there is a strong likelihood that it meets this criteria. If we are recursively solving subproblems of the same problem, then we know that our substructure is optimal, otherwise our algorithm wouldn’t work.
  • Are there overlapping subproblems? This can be more difficult to determine because it doesn’t always present itself with small examples. It may be necessary to try a medium-sized test case. This will enable you to see if you end up calling the same function with the same input multiple times.

Find the Subproblems

If our solution can be made dynamic, the exact subproblems to memoize must be codified. This step requires us to discover the high-level meaning of the subproblems. This will make it easier to understand the solution later. Our recursive solution can be made dynamic by caching the values. This top-down solution facilitates a better understanding of the subproblems which is useful for the next step.

Turn the solution around

We now have a top-down solution. This is fine and it would be possible to stop here. However, sometimes it is better to flip it around and to get a bottom up solution instead. Since we understand our subproblems, we will do that. This involves writing a completely different function (without modifying the existing code). This will iteratively compute the results of successive subproblems, until our desired result is reached.

The following sample problems use the FAST method to arrive at the solutions. By going through the examples, it will help you to understand how each of these steps is applied.

DON'T DO ANOTHER CODING INTERVIEW...

...until you've mastered these 50 questions!

GET YOUR FREE GUIDE

RECOMMENDED ARTICLES

data structures

Article

The Definitive Guide to Data Structures for Coding Interviews

Data structures are critical to coding interview success. In this post, I'll show you exactly which data structures you need to know to nail your interviews.

Article

Acing the Google Interview: The Ultimate Guide

Acing the Google interview. Every engineer's dream. But the Google interview is really hard. In this post, you'll learn exactly how to prepare effectively.
stuck on coding interview

Article

10 ways to get unstuck and never fail another coding interview

It happens time and again. People fail coding interviews because they don’t know what to do when stuck on a problem. Developing a clear plan of attack helps you to succeed at any whiteboard coding interview.
Envelope Twitter Facebook Linkedin Youtube

© Byte by Byte 2016-2022

Privacy Policy

Terms and Conditions

Earnings Disclaimer

What if coding interviews were easy?

Sounds impossible right? It’s not!

Let me show you the RIGHT way to study for interviews so you can ace your Google Interview without breaking a sweat.

Download my FREE guide to the 50 most common coding interview questions asked at companies like Google, Facebooks, and Amazon.

Download my FREE guide to the 50 most common coding interview questions asked at companies like Google, Facebooks, and Amazon.

Get fully prepared for your Coding Interview and save 20% with Exponent. Learn More  →