r/learnprogramming • • 20h 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:)

66 Upvotes

37 comments sorted by

25

u/Ahmed_Hisham0 11h ago • Heartwarming

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.

3

u/dhananjai_31 10h ago

That was the best suggestion đŸ‘đŸ» Tq

17

u/6a70 20h ago • Heartwarming

Anytime I find a new problem I'm stuck
Because I never saw that before

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.

1

u/dhananjai_31 12h ago

Na bro I'm not actually remembering solutions but I'm remembering the pattern i find in the problem And whenever I encounter a problem which I didn't do or never saw I stuck Because I'm unable to create my own logic

6

u/6a70 12h ago

ok: while I disagree with your evaluation, please read beyond the first sentence of my comment

3

u/lgastako 16h 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.

2

u/dhananjai_31 12h ago

Thank u bro

2

u/misplaced_my_pants 9h 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/

29

u/Weekly_Patience685 20h ago

you just have to code more and get more familiar with existing solutions to different problems, its not magic haha

10

u/Miserable_Ad9577 20h ago

LoL do what an AI model would do, scrap more meterial on the topic you want to learn from reputable sources you mentioned and feed yourself those data, eventually you will see patterns emerged

14

u/mxldevs 20h 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

  1. what is your starting point? These are your inputs.
  2. what is your goal? These are your outputs.
  3. what are your constraints? This is the domain that you're working in.

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.

3

u/Zennixi 19h ago

I'm self learning currently and I realized this after 4 months of thinking yes I finally finished all the fundamentals guess what I realized I actually really need to know what's going on beyomd the surface taught levels of thongs for my brain to actually pick up what really happens so I decided to start again with c++ but go deeper under the hood rather than just following tutorials and forcing to build projects prematurely it's taking longer to learn now but I think overtime the knowledge will 2x my speed in a lot of things in the future

2

u/aanzeijar 16h ago

Aren't you just telling him to do the same, just with more puzzle pieces? Because that's what mathematics and coding is at its heart. We just have way more patterns memorised than the average beginner.

2

u/mxldevs 10h ago

It's the difference between

  • solving a bunch of problems and recognizing a pattern vs
  • taking a bunch of patterns and recognizing which problems fits in it

There are many algorithms and formulas that anyone can just grab and apply to solve specific problems, but how many months or years of effort might have went into coming up with those algorithms and formulas in the first place?

At the heart of math and coding is the process of DEVELOPING those patterns.

You can skip a lot of work by just looking it up and applying it, but when you completely skip that process, you will end up in a situation like OP where they just have no idea where to even start when the problem doesn't match any of the patterns they've seen.

1

u/aanzeijar 6h ago

It's tricky yeah. A big problem with teaching beginner coders is that there are a lot of patterns they just can't come up with themselves and it's worse the less of the Spark they have.

If you give someone the task of calculating all primes up to n, most will come up with trial division. Some will remember seeing a sieve somewhere or maybe even realise that sqrt is the upper bound, but pretty much no one will come up with wheel prefill, odd/even compression, cache friendly windows, simd optimisations etc.

And then there's some stuff where you either know the solution or you don't, and for some reason those are really popular with all these stupid gotcha leetcode puzzles that some companies use. And then it rightly just feels like a giant memory game.

9

u/jakingo 20h 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/Senior-Assumption864 12h ago

agree with this, writing the steps in plain words first makes the code part feel a lot less random

5

u/spinwizard69 12h 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.

3

u/Salamanticormorant 8h 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 20h ago edited 19h 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 20h ago

Build from scratch at home, breaking things is inevitable, then find the solution on your own.
Rinse and repeat.

2

u/joranstark018 19h 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/dinanomartin 9h ago

mxldevs makes a solid point about inputs, outputs, and constraints. that framing feels more concrete than pattern matching.

1

u/VanCliefMedia 19h ago

Read psychology of computer programming by Gerald Weinberg

1

u/ruat_caelum 19h 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.

  • Keep in mind some people find a path like this.
  • I have A and Z, I know I'll need J because (reasons) so now I can work A to J and J to Z, etc.
  • You don't have to work in from the ends.

1

u/ledatherockband_ 19h 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 19h 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 19h ago

Mile age will vary, just give it some time. Practice, more practice, more practice and repeat.

1

u/phatbili 16h ago

Callout on how competitive sites train pattern matching and give away hints before you even start thinking from scratch.

1

u/som12313 14h 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:

  1. Everything must start from the very foundation—"Investigating Things and Extending Knowledge" (deeply exploring the essence and first principles of how things work)—and build up step by step before you can tackle complex, large-scale problems ("Governing the State and Bringing Peace to the World").
  2. Right in the center of this loop is "Rectifying the Heart/Mind," which acts as an anchor. Only when your mind is calm, centered, and anchored can you objectively judge what is right or wrong, effective or flawed, instead of panicking when facing an unfamiliar problem.

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!)

1

u/Ok-Interest9727 5h ago

You get better at solving problems by... drumroll... solving problems :)

1

u/Gznork26 4h 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.

1

u/its_shubh_pandey 2h 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

0

u/icchavez 19h ago

Hexagonal thinking skills ARE the silver bullet, bro😎👍