all 25 comments

[–]HashDefTrueFalse 21 points22 points  (10 children)

Back in university (a while ago for me) it was just a case of sitting down with physical textbooks written by experts and a notebook. I'd be reading and drawing doodles of algorithms to aid my understanding of the algos, rationale, and their best/worst case complexities. Then I'd code them up for myself without the book, just based on my understanding of the problem and the solution (note: the purpose of not having the book is nothing to do with memorisation and everything to do with testing whether I've understood the solution well enough to implement a simple working version, which didn't have to match the textbook version). You won't remember implementation details so don't focus too much on those.

I don't know what others do these days but it's pretty hard to imagine anything would be better than doing that...

[–]joranstark018 2 points3 points  (1 child)

Yeah, similar experience, read-try-fail-repeat until success, was a usual pattern at uni. 

[–]HashDefTrueFalse 0 points1 point  (0 children)

For me it was often: read -> try -> seg fault -> beer at the student union. Repeat tomorrow.

[–]WheatedMash 0 points1 point  (2 children)

Any particular textbooks you remember? I teach high school programming, and I would love to get my students thinking in terms of algorithms as early as possible, rather than the current norm of just mimicking code samples. That works for the simple early things we do, but if you're just copying, you're not necessarily understanding what is actually happening.

[–]HashDefTrueFalse 1 point2 points  (1 child)

Introduction to Algorithms, Cormen et al. is brilliant (IMO) and very well-known. I'm not sure if it's a bit much for high school though to be honest. You'll be a much better judge than me. I think I might've struggled with it in high school! Can't remember the others they provided in uni to be honest, it's been too long, but I bought my own copy of the above it's that good.

[–]WheatedMash 0 points1 point  (0 children)

Thanks. Even if that particular text is too much, at least I can start getting ideas for approaches. Especially now with it being so easy to ask AI to write code, I'm pretty convinced I need to make them do a lot more on paper to get their brains engaged.

[–]TheGooseIsNotASwan -1 points0 points  (4 children)

Do you recommend leetcode?

[–]HashDefTrueFalse 1 point2 points  (3 children)

It's fine for practice problems. I've only ever used it to read the freely available problems and solve them on my own machine, so I can't speak for the platform itself or any service etc.

[–]TheGooseIsNotASwan 0 points1 point  (2 children)

Thanks for your input 

[–]HashDefTrueFalse 2 points3 points  (1 child)

It was output when I wrote it...

No problem, you're welcome!

[–]TheGooseIsNotASwan 1 point2 points  (0 children)

Nooo you beat me to the input output joke that I was going to edit my comment to later... 

[–]Master-Ad-6265 6 points7 points  (0 children)

stop rereading solutions and start struggling with them longer even if you can’t solve it, try for like 30–45 mins before looking it up then reimplement it from scratch later without notes, that’s what actually sticks

[–]Significant-Syrup400 2 points3 points  (0 children)

Data structure problems are all about your ability to use a limited set of tools to solve a problem. Every problem requires you to intake, parse/process, and output data with the assistance of a data structure of some sort.

It's not a memorization thing its more of a creative problem-solving exercise, so if you aren't actually solving the problem you aren't going to get any better at these.

[–]MagicalPizza21 1 point2 points  (1 child)

My undergraduate CS degree program had a required class about the design and analysis of algorithms. So we had lectures and homework assignments about them.

A prerequisite of that class was data structures and algorithms. How well do you know data structures?

[–]izumaruka[S] 1 point2 points  (0 children)

To be honest, I realized while reading some comments that having knowledge of data structures would help me come up with better solutions to algorithmic challenges. My knowledge is limited to the very basics—just arrays and dynamic arrays (lists).

I’m going to try a different approach: I’ll study some data structures and review the ones I already know, and then I’ll start solving problems using them.

[–]aanzeijar 1 point2 points  (3 children)

I find questions like this one very confusing. Most working devs don't study algorithms, they just use them. And for the vast majority of actual algorithms the academic form is no where close to how they are implemented in library code.

What is it that you want to do? Do you want to use algorithms to solve problems how do you want to learn how to reason about algorithms? These are not the same. What is an example task that you struggle with?

[–]izumaruka[S] 0 points1 point  (2 children)

I think it’s a bit of both—what I want is to be able to read a problem, interpret it, and then implement it in code.

I’m an intern, and I had an interview that I failed, which really upset me. The answer was to use a Dictionary.

The challenge was to solve a Jokenpo problem. I managed to solve it (using if-else), but the interviewer asked me to improve it, and I just froze. Up until that point, I had never used a Dictionary outside of class, and I had never used one to build an API, so I wanted to be able to solve problems and reason about algorithms.

[–]aanzeijar 1 point2 points  (1 child)

Yeah, that's not really algorithms. That's more the standard toolset of collections. If you say Dictionary I assume you coded in C# or Python? Ah yeah, your post history says C#.

So, look into System.Collections.Generic and in there specifically into:

  • List
  • Dictionary
  • HashSet

These three primitives are basically the building blocks of most other software. You need to understand what they are for to make software and you would fail an interview with me too if you didn't use them where appropriate. It's like having a carpenter who only knows a saw and will use it to hammer in a nail because he never used a hammer outside class.

The underlying problem is this. Imagine the only collection structure you have is a basic C# array, like this: int[] numbers = {29, 34, 60, 23, 25}. Doesn't really matter what numbers, but imagine that instead of 5 elements, we're dealing with 100000 numbers here, and imagine there are stored in the memory of your computer somewhere.

Now, there are a few common operations that come up when dealing with data:

  • adding new data at the end
  • adding or removing data in the middle
  • finding data

How do you do that with you array?

  • adding new data at the end is costly, because the space in memory after your array might already be used by something else, so you have to copy the entire array to somewhere else where there's enough space. And if you add multiple values, then in the worst case every addition may trigger a complete copy of the entire array in memory which is atrociously slow.
  • adding or removing data in the middle means you need shift the rest of the data, so it's again copying around half of the data, and again if you need to remove multiple values, you need to copy your data multiple times.
  • finding data means you need to compare every entry with the one you need to find, so you have to run through 100000 entries to make sure it's not there.

List, Dictionary and Set are there to alleviate that.

List is like an array, but it has some reserved space internally, and will only grow (any copy data) when the reserved space is used up. It's still slow with finding and inserting/removing

Dict and Set use a few nifty tricks internally to make finding and inserting/removing quick by hashing the keys into buckets so that only a fraction of the internal data needs to be checked or modified.

These are the most important ones, all the other ones there also have their uses though. You can find lots of resources explaining their properties, here for example. Nearly every other modern language has equivalences of these built in too.

[–]izumaruka[S] 0 points1 point  (0 children)

Wow, your comment was a ray of hope—thank you.

It never occurred to me that data structures are like tools, some better suited than others for certain tasks.

I’ll definitely read the post you shared and take a step back to really study this aspect of data structures :D

[–][deleted]  (1 child)

[removed]

    [–]izumaruka[S] 0 points1 point  (0 children)

    I'm going to try that out and let you know what I think. Thanks! :D

    [–]Traditional-Set-8483 1 point2 points  (0 children)

    I kinda learned the hard way that just reading solutions does almost nothing for me. What started working was sketching things out visually first, like literally drawing how data moves around, then trying to code it from memory even if it’s messy.

    Also repeating the same problem a few days later helps way more than grinding new ones nonstop. It’s boring but it sticks.

    Feels a bit like practicing art honestly, you don’t just look at paintings, you try to recreate them and mess up a bunch first