Are you thinking about how you can improve your problem-solving skills or logic in coding? Or how can you start feeling more confident and comfortable in solving technical challenges and expressing your ideas clearly and adequately, especially in an interview?
After that, did you face this situation before; reading a technical question and saying yes, but it’s too easy to be solved, then when you start, you complicate things and end up starting everything from scratch or even give up and stop trying!
If yes, in that case, let me give you some simple steps to apply to every question, from the easiest to the most difficult ones.
Our golden Rule: Do Not rush and just dive into coding.
- Read the problem (question) carefully, and define the problem. Why? Because if you do not know what is the required result, you will not realize when you have successfully solved it, which means you will be wasting more time and effort than needed.
- Read the question again and make sure you can determine what it is:
- Input
- Output
- Edge cases -if we have any – (any assumption that might break your code or make it behaves in a different way than expected)
- Constraints -if we have any- (time complexity, not using specific methods or built-in functions….etc.)
- Have a clear (step-by-step) plan before you start the implementation. How?
- Write a Pseudo code:
i. Reword the problem in plain English. Above all, If you can explain the problem to someone else in plain English, this means you understand it.
ii. Also, you will have the chance to slow down and think carefully about the steps your program needs to follow to solve the problem.
iii. (slow down to speed up), As much as you put effort into your pseudocode, you will save a lot of time in writing the code itself.
See this example:
Write a function called add numbers that will take two numbers as parameters and return the summation as a result //Pseudo code /* StartGet two numbers (declare two variables representing the) : First numberSecond number Add them Print the answering. */ |
- Divide and conquer. To explain, divide the big problem into subproblems, which leads to less complexity and makes the problem easier.
- Step-by-step implementation:
- In the first place declare the main function body with the required variables (inputs and outputs);
- For instance, use meaningful names for the functions and variables that reflect what each function will do;
- Create helper functions whenever it’s needed (may help for the subproblems);
- By all means, ideally, each function should have a single (purpose, job or functionality) to do;
- And Finally, Test as you go, do not wait till the end to see if your code is working fine.
- In the first place declare the main function body with the required variables (inputs and outputs);
Testing each step in problem-solving will save you hours of coding, debugging and searching where the error occurred.
Now you may be wondering where we should do all these steps (whether when practising or in an interview?!!!)
In a word simply use the White Board (paper, online whiteboard, real whiteboard, etc.) and divide it into three columns:
- First column: Input, Output, EdgeCases and Constraints;
- Second column: Pseudocode;
- Third column: the code itself 😉
See this demonstration for the previous example on a whiteboard
I: Two numbers O: number (sum of two numbers) E: none C: none | Get two numbers (declare two variables representing the) : First number second number Add them Print the answer | function addnumbers(){let n1 = Number(prompt(“Enter first number”));let n2 = Number(prompt(“Enter second number”)); return(n1+n2);} |
Finally, you might be following other techniques, which is fine and right. But always remember to arrive at the Z point from the A point. Therefore, you need to go through B, C and D to all the points in between, so do not rush and dive into coding immediately.
Source: https://www.geeksforgeeks.org/how-to-write-a-pseudo-code/