How to Prepare
1. Listen Carefully
Be sure to record any unique information in the problem.
For example, if the question starts with "Given two arrays that are sorted", the optimal algorithm for the sorted solution is probably different from the unsorted one.
2. Draw an Example
You have to create an example that is specific (that uses real numbers or strings), sufficiently large and as general as possible.
3. State a Brute Force Algorithm
Even if it's obvious for you, it is not necessarily obvious for all candidates. Despite being slow, explain the 1.1 Big O#⏱️ Time complexity and the 1.1 Big O#💾 Space Complexity of the algorithm.
4. Optimize
- Be sure to use all the information given at 1.2 How to Prepare#1 Listen Carefully.
- Storing extra state about the problem or precompute information could help.
- Consider using Hash Tables.
- Think about 1.3 Optimize and Solve Technique
5. Walk Through
Solidify your understanding of the algorithm. If you made some mistakes in the previous parts and you start coding, it will take longer to fix first the algorithm, and then the code.
6. Implement
Do this preferably on paper.
Remember:
- Modularized code
- ❌
int** matrix = { {1,2,3}, {4,5,6}, ...} - ✅
int** matrix = initMatrix(int size)
- ❌
- Use //TODO and just explain out loud if needed
- Good variable names
- ❌ a, b, c
- ✅ startChild (even better if you abbrieviate it after the first usage)
7. Test
Hand testing is very slow
Remember:
- Just read and analyze what each line of code does.
- Double check weird looking code and hot spots like:
- x = length - 2
- for(int i = 1; ...)
- Base cases in recursion
- null nodes
- Small test cases.
- Special cases (null or single element values ecc.).