filmov
tv
Coding Interview Prep | 3 MUST KNOW Graph Problem Tips

Показать описание
Ace your next coding interview with these 3 graph problem tips! Graph problems are an integral part of coding interviews and the examples I go through will help you solve them faster and easier!
Check out my interview prep platform for learning the patterns!
These are 3 must know graph problem tips (with examples) that are going to help you in your next coding interview. The first tip is to know what data type to use when you are writing your search algorithm. There are a couple of options including using a class, string, or an array; however, the best way is to use just an integer. We make a 2D to 1D mapping using simple conversion formulas which end up reducing the amount of code we have to write.
The second tip is relating to how we implement typical search algorithms. In order to perform a search algorithm such as a breadth-first search or depth-first search, we must explore our neighbors in every iteration. Instead of checking each neighbor one by one, we can create a 2D array containing all of the directions that we want to check, then simply loop over all of these directions and perform the checks in one place. This simplifies our code and allows to write our solution must faster.
The last tip is under the scenario that our input is restricted, we can use our input as our visited set. In normal search algorithms, we maintain a structure to keep track of places we have already been to, but this is unnecessary if our input is restricted in some way. All we need to do is change the elements to a value outside of the bounds of our restriction.
Check out my interview prep platform for learning the patterns!
These are 3 must know graph problem tips (with examples) that are going to help you in your next coding interview. The first tip is to know what data type to use when you are writing your search algorithm. There are a couple of options including using a class, string, or an array; however, the best way is to use just an integer. We make a 2D to 1D mapping using simple conversion formulas which end up reducing the amount of code we have to write.
The second tip is relating to how we implement typical search algorithms. In order to perform a search algorithm such as a breadth-first search or depth-first search, we must explore our neighbors in every iteration. Instead of checking each neighbor one by one, we can create a 2D array containing all of the directions that we want to check, then simply loop over all of these directions and perform the checks in one place. This simplifies our code and allows to write our solution must faster.
The last tip is under the scenario that our input is restricted, we can use our input as our visited set. In normal search algorithms, we maintain a structure to keep track of places we have already been to, but this is unnecessary if our input is restricted in some way. All we need to do is change the elements to a value outside of the bounds of our restriction.
Комментарии