Approaching Algorithms
Much like learning a new language, developing strong algorithmic thinking skills requires practice, patience, and a good understanding of best practices. There is no single "right" way to approach every problem, but there is a highly effective methodology that helps scripters reason through challenges systematically. The following steps outline a best-practice approach to algorithmic thinking, which, when followed, can guide you through the process of understanding, designing, implementing, and testing an algorithm in a clear and efficient manner.
-
Step 1: Understand the Problem
-
Step 2: Draw an Example
-
Step 3: Sketch a Solution
-
Step 4: Optimize Efficiency
-
Step 5: Walk Through the Algorithm
-
Step 6: Implement the Algorithm
-
Step 7: Test the Implementation
1: Understand the Problem
The goal of this step is to listen, be curious, and identify clues. Before jumping into any solution, it's critical to first understand the problem. Carefully read through the problem statement, then parse out important details and constraints. If necessary, ask questions to clarify any ambiguities and confirm your assumptions. For a written material such as these examples, use the example test cases to clarify any ambiguities. There is no further complication to what is given.
2: Draw an Example
The goal of this step is to exemplify one's understanding by creating a custom example of specific input and output data. This helps to concretize the abstract problem and may reveal patterns that may not have been initially obvious. Testing with a self-designed example ensures the depth of understanding required for the next steps.
3: Sketch a Solution
The goal of this step is to outline a possible approach, even if it's inefficient. Once there is a clear understanding of the problem and a concrete example to refer to, the next step is to sketch, or pseudocode, a solution. This solution does not need to be optimized or refined at this point, nor need it be working software; the goal is to lay out the basic steps needed to solve the problem. Even if the solution is inefficient, having an overarching idea of how to proceed is more important than worrying about speed or resource usage at this stage.
4: Optimize Efficiency
The goal of this step is to remove bottlenecks, duplicated work, and inefficiencies. Once a working solution is in place, it is time to think about optimization. One should identify any parts of the solution that may lead to unnecessary computation or resource consumption. Bottlenecks, duplicated work, and inefficiencies should be removed to streamline the solution and improve performance, especially for scaling to larger input datasets.
5: Walkthrough the Algorithm
The goal of this step is to ensure one's understanding by testing the general algorithm by a deliberate step-by-step examination with example data. This allows one to check that the logic flows correctly and helps uncover any issues with the design before implementing the steps in code. This walkthrough ensures that one fully understands how the algorithm behaves in practice.
6: Implement the Algorithm
The goal of this step is to write valid software code which satisfies the requirements. Focus on writing clean, well-organized code. The code should be written clearly with well-named variables. Feel free to use secondary methods or functions to extract large sections of logic and to break the problem into smaller, more manageable components. Modular code is more readable and maintainable. A modular approach also makes future modifications easier and faster.
7: Test the Implementation
The goal of this step is to test the written code with test data and software tools. Use a variety of test cases, including edge cases to ensure that the code handles all possible scenarios. The programmer should walk through the code manually with these test cases, and, if desired, use software testing tools to verify that the implementation works as expected in all cases.
A Note on Algorithmic Efficiency
While Step 4---Optimizing Efficiency---is an important step in algorithm design, it is an advanced technique that may not be immediately necessary for beginners. For those just starting out, focusing on understanding the problem and developing a working solution is more important than perfecting efficiency.