Algorithms Flashcards
What is an algorithm?
A process or set of steps to accomplish a certain task.
Why do I need to know algorithms?
It is the foundation for being a successful problem solver and developer.
What are the 5 steps to take to best solve a problem?
- Understand the problem
- Explore concrete examples
- Break it down
- Solve/Simplify
- Look back and refactor
When solving a problem, what are the 5 steps/questions that can help to better understand the problem?
- Can I restate the problem in my own words?
- What are the inputs that go into the problem?
- What are the outputs that should come from the solution of the problem?
- Can the outputs be determined from the inputs of the problem. or do I have enough information to solve the problem?
- How should I label the important pieces of data that are part of the problem?
When solving a problem, what are the 4 steps to take when exploring examples?
- Start with simple examples 2. Progress with more complex examples 3. Explore examples with empty inputs 4. Explore examples with invalid inputs
When solving a problem, how do you break down the problem? And why would you break it down?
By explicitly writing out the steps you need to take, in pseudocode or comments. It forces you to think about your approach and the code you are going to write before you write it, and helps you catch any lingering conceptual issues or misunderstandings before you dive in and have to worry about details (e.g. language syntax) as well.
When solving a problem and getting stuck, what are the 4 steps to take to simplify the problem?
- Find the core difficulty in what you are trying to do 2. Temporarily ignore that difficulty 3. Write a simplified solution 4. Then incorporate that difficulty back in
When solving a problem, what are the 5 ‘look back and refactor’ questions to ask yourself?
- Can I check the result? 2. Can I understand it at a glance? 3. Can I improve the performance of the solution? 4. Can I derive the result differently? 5. Can I think of other ways to refactor?
In JavaScript, how to use a regular expression to check if a character is an alphanumeric character?
/[a-zA-Z0-9]/.test(character)
Returns true if it finds a match, otherwise it returns false Note: Regular expressions do not have the best performance; checking ASCI character code value (using string.charCodeAt(index)) could be about 50% faster.
Name 7 of the most commonly used problem solving ideas or patterns.
- Frequency Counter
- Multiple Pointers
- Sliding Window
- Divide and Conquer
- Dynamic Programming
- Greedy Algorithms
- Backtracking
What is the idea behind Frequency Counters,, and how do they improve performance?
The Frequency Counters pattern uses an object or set to collect values/frequencies of values.
They often avoids the need for nested loops or O(n^2) operations with arrays/strings.
What is the idea behind the Multiple Pointers pattern, and how do they improve performance?
The Multiple Pointers pattern creates values (pointers) that correspond to an index or position, and move towards the beginning, end or middle based on a certain condition.
This pattern is very efficient for solving problems with minimal space complexity.
What is the idea behind the Sliding Window pattern, and what is it useful for?
The Sliding Window pattern involves creating a window which can eiter be an array or number from one position to another. Depending on a certain condition, the window either increases or closes (and a new window is created).
The Sliding Window pattern is useful for keeping track of a subset of data in an array/string etc.
What is the idea behind the Divide and Conquer pattern, and what is its biggest advantage?
The Divide and Conquer pattern involves dividing a data set into smaller chunks and then repeating a process with a subset of data.
The Divide and Conquer pattern can tremendously decrease time complexitiy.
What is a recursive function, and how does it affect the call stack?
A recursive function is a function that keeps calling itself until it reaches a base case. A recursive function keeps pushing new functions onto the call stack.