Skip to content

Latest commit

 

History

History
39 lines (33 loc) · 2.27 KB

File metadata and controls

39 lines (33 loc) · 2.27 KB

Algorithmic Toolbox

Have you ever given a programming problem and freeze without knowing where to start? Well, in this section, we are going to give some tips so you don’t get stuck while coding.

Tip
TL;DR: Don’t start coding right away. First, solve the problem, then write the code. Make it work first; make it better later.
Steps to solve algorithmic problems
  1. Understand the requirements. Reframe it in your own words.

  2. Draw a simple example (no edge cases yet)

  3. Brainstorm possible solutions

    1. How would you solve this problem manually? (without a computer) Is there any formula or theorem you can use?

    2. Is there any heuristics (largest, smallest, best ratio), or can you spot a pattern to solve this problem using a greedy algorithm?

    3. Can you address the simple base case and generalize for other cases using a recursive solution? Can you divide the problem into subproblems? Try [Divide and Conquer].

    4. Do you have to generate multiple solutions or try different paths? Try [Backtracking].

    5. List all the data structures that you know that might solve this problem.

    6. If anything else fails, how would you solve it the dumbest way possible (brute force). We can optimize it later.

  4. Test your algorithm idea with multiple examples

  5. Optimize the solution –Only optimize when you have something working. Don’t try to do both at the same time!

    1. Can you trade-off space for speed? Use a [hashmap-chap] to speed up results!

    2. Do you have a bunch of recursive and overlapping problems? Try [dynamic-programming-chap].

    3. Re-read requirements and see if you can take advantage of anything. E.g. is the array sorted?

  6. Write Code, yes, now you can code.

    1. Modularize your code with functions (don’t do it all in one giant function, please 🙏)

    2. Comment down edge cases but don’t address until the basic cases are working.

  7. Test your code.

    1. Choose a typical input and test against your code.

    2. Brainstorm about edge cases (empty, null values, overflows, largest supported inputs)

    3. How would your code scale beyond the current boundaries?

These steps should get you going even with the most challenging algorithmic problems.

Stay effective!