r/learnprogramming • u/dhananjai_31 • 2h ago
How to improve logical thinking in programming
Hey
I'm 23m working professional,
Yesterday at the office my team lead said he found a pattern in my work
He said I'm mugging up patterns and not thinking logically.
Well I do codeforces , leetcode, hacker rank to improve my programming skills but I understood that he was right
All I do is find a pattern in the problem and try to solve it
Anytime I find a new problem I'm stuck
Because I never saw that before
How to come up with my own solution?? How to improve problem solving and logical thinking???
Open to all the suggestions:)
5
Upvotes
6
u/Weekly_Patience685 2h ago
you just have to code more and get more familiar with existing solutions to different problems, its not magic haha
5
u/6a70 1h ago
This indicates that you're actually just remembering solutions.
You need to practice brute forcing things. And if the first solution you come up with isn't brute force, you still need to find out how to brute force it.
Go to TwoSum and brute force it—yes, the obvious n^2 solution. Then find the other n^2 solution that involves "inspecting the numbers we've already seen". Then take that and optimize that lookup with a hash table.
That's the approach—always brute force first. All of the solutions you come across in LeetCode are direct optimizations on brute force.
3
u/mxldevs 1h ago
The problem with pattern matching is that you're just taking a dozen different puzzle pieces and just trying each one to see if it fits.
When none of them work, you throw your hands up in the air and go "well I've tried everything I could and have no idea what to do"
You want to get better at solving problems? You go back to your fundamentals
Then you figure out your base cases. If your input is an array of objects, what happens when you have an empty array? What happens when you have one element? When you have more than one element?
You're told that you lack logical reasoning, which means you don't know how to approach a problem. Throwing puzzle pieces at it and hoping something sticks is a bad habit to develop.
2
u/jakingo 1h ago
Try solving problems by breaking them into smaller steps before looking for patterns or solutions. Explain your logic in plain english first and then turn it into code. This builds independent thinking.
2
u/Boogieduzit1312 1h ago
Build from scratch at home, breaking things is inevitable, then find the solution on your own.
Rinse and repeat.
•
u/joranstark018 46m ago
My advice to junior devs is to ask questions, to explore more and to reflect on their solutions (eg what worked, what did not work, what can be improved) not just to settle with the first solution that works.
I would ask my team lead for advice on how I can improve my deliveries, I would discuss programming problems with my team members, I would present design drafts to get input from the team (and relevant stakeholders) before I start implementing. It's a teamwork.
1
u/AstralIsles 1h ago edited 1h ago
Solving an arbitrary problem is a wide question. Do you have a specific one or class in mind?
I ask myself what a brute force solution might look like.
Then I ask myself what an optimized algorithm could possibly look like.
At this point I am just thinking about the Big O for space and time.
What would the parameters of the function look like.
If the best I can imagine an algorithm to be is N, then obviously I need to loop over everything.
I consider what kind of data might I need to save to reduce having to look back. I consider what data structures are needed.
There are different tools one can add to their tool belt such as dynamic programming, iterating from both end of the data, leaving behind data so that you can backtrack if called for.
Then I think through edge cases: what will overflow, what will happen at 0 or 1, ect.
1
u/icchavez 1h ago
Hexagonal thinking skills ARE the silver bullet, bro😎👍
•
u/VanCliefMedia 48m ago
Read psychology of computer programming by Gerald Weinberg
•
u/ruat_caelum 45m ago
The long answer is "baby steps"
I'm at point A I need to get to Point Z. I know those two points. What step can I take from A OR what step can I work backwards from Z to bridge that gap.
Keep in mind that because you can't (at that time) visualize a full path you will make partial paths that don't work and have to back track.
Eventually you come up with some path A, J, R, S, T, W, Y, Z
Then you have to evaluate it for Good-in-good-out. Will it PROVABLY produce good results if you give it good inputs.
What is a "Good input" (list them out) and what assumptions are you making about them. E.g. "person's name" is a good input but the assumption is that they only have a "first + middle + last" name and not more, which many people do in non-western countries.
Then make sure you reject any input that "isn't good" and document what assumptions / checks / assertions you are making.
Then you refine the path.
Is your R,S,T block some sort of data handling you can offload to a more efficient native data structure? etc.
•
u/ledatherockband_ 33m ago
Get familiar with architectural patterns and concepts.
Often times, developers usually focus on line-by-line logic and miss how the pieces fit together. Systems thinking is what you should be thinking about, stuff like domain driven design, inversion of control, dependency injection, or whatever pattern your work follows. Ask Claude to break it down for you and study that.
Also, bad on your senior for not telling you how exactly your logic is bad.
•
u/Willbo 27m ago
This is a very common problem many engineers run into early on. Often times they join a job thinking that it should be a certain way and they begin coding a solution before understanding the problem fully.
There is a popular saying of Chesterton's Fence - "Don't tear down a fence until you know why it was put up." In this case you are building something rather than tearing it down, but the point is you should understand the purpose of a structure before you begin making changes.
The most important stage of engineering solutions is drafting requirements. If you start coding without understanding the stakeholders, use cases, and constraints for your project, you will most likely build something nobody asked for or something that isn't right-sized for its purpose.
Most times you should also adhere to the structure that has already been laid out for you as well. Do not try to reinvent the wheel, borrow from the genius of engineers that have already built solutions for your org, because they understand the real constraints the org faces.
While some designs work in theory, making them practical in an org can surface unaddressed requirements, become complex and bloated, run into scalability issues, or become very expensive. Don't forget the solution also has to be maintained and live with the org. If you can fulfill the underlying requirements without even building code or leaving a trace, you have built the cleanest solution of all.
•
u/a-dot-in-the-sky 19m ago
Mile age will vary, just give it some time. Practice, more practice, more practice and repeat.