How much time should I spend on a difficult DSA problem before reading its editorial?
Often I am asked this question by our students at Programming Pathshala, how much time they should spend on a problem, if they find it difficult to solve?
Hi, my name is Bharat Khanna and I am one of the Co-Founder here at Programming Pathshala. During my preparation of coding interviews, I used to divide problem solving into following parts -
- Reading and understanding the problem — Lets take an example problem.This problem looks very complicated by its statement. But, if you calmly try to understand it, you will get that it just wants you to look at all the query subarrays for different [l,r]. Those subarrays which have a positive sum should be taken in answer and their sum is to be added to total answer.
- Write a brute force solution to it. This would be a very basic solution to the problem. In this case, you would just loop on all queries, take sum of the subarray for that query. If it is positive, add it to answer.
- Optimise it using DSA. Here comes your ability to look for patterns which indicate using certain Data Structures and Algorithms. Like in this problem, because we are looking at subarray sums and recalculating it in each query, we can precompute it using prefix sum and answer each query in O(1).
You should analyse during your preparation the weak part.
- If part 1 is weak, try to practice reading multiple questions and dry running your understanding on sample cases.
- If part 2 is weak, do more easy problems on platforms to be able to at least think brute force for each and every problem.
- If part 3 is weak, you need to work more algo by algo to make sure that you know and map different patterns to different algorithms.
To answer your question, ponder hard and try to atleast reach till part 2 always. Then if you are unable to improve beyond a certain point in part 3, ask for hints if you have mentorship. If not, go for the editorial but note down the pattern you missed and make sure the pattern gets mapped for future
Hope you find it useful for when you start solving a problem next time.
For more information, you can also login to www.programmingpathshala.com and take a free trial today.
Have a doubt? Reach out to us at email@example.com