r/learnprogramming • u/dhananjai_31 • 4d 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:)
167
Upvotes
71
u/Ahmed_Hisham0 4d ago •
your lead is describing the difference between recognising a solution and deriving one, and the gap is real. the good news is it's trainable and it's not about being smarter.
the drill that works, and it's boring:
solve the problem, then delete your code entirely and redo it the next day with nothing open. not from memory of the code, from a blank file. you'll find out immediately whether you understood it or just reproduced it.
before writing any code, write the solution in plain english, in sentences, and specifically write down what stays true the whole time (the loop doesn't lose track of X, the stack holds the ancestors, whatever). that sentence is what transfers to a new problem. the code isn't.
when you're stuck, force a prediction before running anything. say out loud what you think the output will be and why. being wrong is the useful part, it's the only thing that fixes the mental model.
and go the other way too: take a problem after you've solved it and change one constraint. give it a duplicate, a cycle, a bigger input. the pattern you memorised breaks immediately and you have to actually reason.
leetcode is fine for the recognition part. the derivation part needs you to be uncomfortable for 20 minutes, and grinding more problems doesn't do that for you.
20
u/6a70 4d 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.
4
u/lgastako 4d ago
I would recommend watching this MIT lecture series: https://www.youtube.com/watch?v=2Op3QLzMgSY
In it, they introduce the concept of programming by "wishful thinking" which is really just a technique for constructing a solution to a problem in logical pieces.
I think they introduce it in the first video but most of the discussion about it is in the second, though it's been long enough that I might be wrong about that. Either way, the whole thing is worth watching if you want to get good at the craft.
3
u/misplaced_my_pants 4d ago
Work through Math Academy (and/or a book on proofs) and this book: https://htdp.org/2026-5-28//Book/index.html
This book is also great: https://logicforprogrammers.com/
18
u/mxldevs 4d 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.
33
u/Weekly_Patience685 4d ago
you just have to code more and get more familiar with existing solutions to different problems, its not magic haha
10
u/jakingo 4d 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.
5
u/spinwizard69 4d ago
personally I see things like Leetcode, Hacker rank and others, as a total waste of time for developing usable skills. So stay away from those places.
Often the problem here is the same as math student have with solving word problems. I'm not sure what is a good pattern to follow for breaking a problem down into code. But often the first step is the breaking of the problem down.
1
u/Possible-Clothes-891 3d ago
you team didn't use ai ïŒ
3
u/Salamanticormorant 4d ago
One way to avoid something that sometimes trips me up is to write code as if I wasn't going to use a loop. Once I've repeated a few lines, then I decide what should be before the loop, in the loop, and after the loop. It helps with things like getting a correct count of iterations, whereas I tend to be off by one if I don't do that.
2
u/AstralIsles 4d ago edited 4d 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.
2
u/Boogieduzit1312 4d ago
Build from scratch at home, breaking things is inevitable, then find the solution on your own.
Rinse and repeat.
2
u/joranstark018 4d 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.
2
u/som12313 4d ago
Your team lead is spot on. What youâre experiencing right now is a lack of "self-consistency" in your thinking framework.
Itâs just like people in ancient times who only memorized books by rote without truly understanding the meaning behind the words. Youâve memorized a lot of coding patterns, so you recognize them when they look familiar. However, because you haven't grasped the underlying principles, the moment a problem is asked in a slightly different way or you face something completely new, you get stuck and can't express a solution. This isn't an emotional blockâitâs simply that your foundational thinking architecture hasn't been built yet.
The most fundamental way to fix this is to fill in the missing "cognitive nodes" in your mind and build a complete, connected thinking system.
You can borrow a classic Eastern mental model from the Confucian text "The Great Learning" (Da Xue). It outlines eight interconnected steps: Investigating Things, Extending Knowledge, Sincerity of Thought, Rectifying the Heart/Mind, Cultivating the Self, Regulating the Family, Governing the State, and Bringing Peace to the World. These aren't rigid rules; they actually describe a self-consistent, closed-loop architecture for how human thought should be structured and how we make judgments:
Iâm sharing this framework so you can use it as a blueprint for how to learn programming. Right now, memorizing patterns keeps your thinking "flat," like a 2D sheet of paperâit only works on problems you've already seen. Once you connect these underlying cognitive nodes step by step and build your own logical structure, your mind transforms into a layered, deep "3D source of thought." From there, no matter what new problem comes your way, youâll be able to derive your own solutions from the ground up.
(Note: English is not my native language and I can't read English, so I used an AI translation tool to express what I wanted to say. If anything sounds a bit blunt or awkward, please forgive me!)
2
u/dinanomartin 4d ago
mxldevs makes a solid point about inputs, outputs, and constraints. that framing feels more concrete than pattern matching.
2
u/Gznork26 4d ago
I found that lateral thinking helped a lot, because it got me used to looking at a problem from several directions, breaking it down in different ways, and taking several stabs at a solution before picking one to pursue further. I've also added stating with the intended result and looking for ways to work backwards towards the original problem state.
That's how I managed to find ways to misuse things in creative ways that made a solution possible.
2
u/profalaxiz 2d ago •
Honestly, props for actually taking your team lead's feedback seriously instead of getting defensive. LeetCode and Codeforces accidentally train a lot of people to do pure pattern matching, which falls apart the second you hit messy real-world requirements.
I think the biggest fix is forcing yourself to start from brute force on paper before you ever touch the keyboard. When you see a new problem, skip trying to guess which memorized template fits it. Write out the dumbest, slowest way to solve it first, then trace a small example by hand to see where the wasted work is. Once you spot the bottleneck yourself, the right data structure or optimization usually becomes obvious.
2
u/nolan5050 1d ago
The problem with pattern grinding is that real bugs don't come with a tutorial telling you which pattern to use. Pick one tiny example and work through it properly. Codecademy leans pretty heavily on guided learning while Boot dev makes you write code and checks whether it actually works.Â
1
u/VanCliefMedia 4d ago
Read psychology of computer programming by Gerald Weinberg
1
u/ruat_caelum 4d 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.
1
u/ledatherockband_ 4d 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.
1
u/Willbo 4d 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.
1
u/a-dot-in-the-sky 4d ago
Mile age will vary, just give it some time. Practice, more practice, more practice and repeat.
1
u/phatbili 4d ago
Callout on how competitive sites train pattern matching and give away hints before you even start thinking from scratch.
1
u/Ok-Interest9727 4d ago
You get better at solving problems by... drumroll... solving problems :)
1
u/its_shubh_pandey 4d ago
You don't need to start lead code or hacker rank i would suggest practice the basics learn basics then start applying then in your day to day work
1
u/lumberjack_dad 2d ago
How did you do with abstract logic in math classes?
0
u/icchavez 4d ago
Hexagonal thinking skills ARE the silver bullet, brođđ