r/learnprogramming • • 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

48 comments sorted by

71

u/Ahmed_Hisham0 4d 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.

8

u/dhananjai_31 4d ago

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

2

u/variable_genitals 3d ago

pick one you did last month and blank slate it again the ones that feel easy now are usually the memorized ones man

20

u/6a70 4d 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 4d 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

7

u/6a70 4d ago

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

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.

2

u/dhananjai_31 4d ago

Thank u bro

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

  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 4d 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 4d 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 4d 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.

2

u/aanzeijar 4d 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.

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

11

u/Miserable_Ad9577 4d 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

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.

2

u/Senior-Assumption864 4d ago

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

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 

1

u/dhananjai_31 3d ago

We work on bank data So we are in a controlled environment With limited ai use Also we do our job on a particular application and ai can't give correct code according to that application

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:

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

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 • Bravo

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.

  • 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_ 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

2

u/DorothyFiore 4d ago

This is vague advice. "Practice the basics" is exactly how he ended up mugging up patterns in the first place... He needs to sit with a problem 45+ minutes before touching the editorial, then ask why the solution works.

1

u/its_shubh_pandey 4d ago

No it's not vague bro. Focus on basic what I think is important because once everything is clear start practicing with real problem how you will retrieve the information from data base short it improve time complexity then spaces then try it with using algorithm like binary search sorting. Once you know how to apply algorithm you will be a very good developer because I have seen in my i struggle know which algorithm to use. Currently I'm improving little by little

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😎👍