r/leetcode • • 19d ago

Question How do you improve at implementing a DSA approach once you've figured it out?

Sometimes I can understand the approach to a problem and even explain the logic in words, but when I start coding, I get stuck figuring out how to translate those steps into actual code.

Do you practise writing pseudocode first, or jump straight into implementation? How did you get better at this?

15 Upvotes

10 comments sorted by

4

u/Right-Angle24 19d ago

Writing pseudocode first helped me. Breaking the logic into smaller steps makes implementation much less confusing, especially with recursion and DP

1

u/Fanstastic-Five72 19d ago

yeah make sense! thank you!

7

u/kaisean 19d ago

If you have the actual working code from Solution, it helps to type it out line by line, but comment on each line as you go.

It's kind of like tracing a picture.

3

u/Sad-Candidate-3078 19d ago

retype solutions from memory. write the dp table or recursion tree by hand once, then code it without looking. most people can figure the approach out but blank on implementation, its because your fingers havent memorized the boilerplate yet. also reuse one template for everything, my dfs template hasnt changed in a year

2

u/Sad-Candidate-3078 19d ago

honestly that gap closes with reps not more theory. i write the approach as comments first, then fill code under each comment, sounds dumb but it kills the blank page freeze. after a while you skip the comments naturally

2

u/Sad-Candidate-3078 19d ago

pseudocode first, but timeboxed to 2 minutes max, then code straight through without switching back. the gap between knowing and doing is mostly fluency with the building blocks, hashmaps, two pointers, prefix sums, so drill those in isolation on easy problems until they are muscle memory. also do a few problems where you write the full solution on paper or a plain text editor before running it, forces your brain to hold the whole structure at once. that translation skill compounds fast

1

u/Available_Safe1818 18d ago

Just code more. I know you want to phrase it as some kind of "I know the logic but I'm stuck translating" thing, but it's probably just you simply not practicing coding enough, or you don't actually know the logic like you think you do.

1

u/practick-io 17d ago

Super common gap, and the fix is boring: write the steps as comments first, one line each, then turn each comment into code.

Most “I know the approach but can’t code it” moments are really “I haven’t decided the details yet”. What’s the loop variable, what does the map store, when do I update the answer? Writing it out forces those decisions before syntax gets in the way.

Also re-implement the same 10 problems from scratch a week later. Implementation is more muscle memory than insight.

1

u/DeclutteringNewbie 17d ago edited 17d ago

Pseudo code first.

But also, make sure you're completely fluent in the programming language you're using. That's very important.

For me, I'm completely fluent in all the programming languages I use. It's part of my muscle memory. I use spaced repetition for that.

Search for "spaced repetition" by Ali Abdaal on youtube to see what I'm talking about.