r/compsci • u/kavinda14 • May 18 '20
Share your techniques of solving problems
Any interesting new techniques anyone follows? Even old are current ones are welcome :) Just want to see how more experienced programmers approach problems.
Also, any good books on this topic that are a must to read?
Edit: the type of problem here is open to interpretation. I think there is a similar pattern that can be applied to problems of any type.
116
Upvotes
7
u/Kinglink May 19 '20
I'd need a lot more information on this. Are we talking about Academic problems, Hacker Rank (skill test) problems, real world problems, or compiling issues?
So each one is different.
Academic problems, I usually just think "What do they want to hear?" Look at your last lesson, use that, get A, move on.
HackRank (or interview problems) I usually start looking for the edge cases, so I can identify those ahead. There's a reason they are asking this "sounds simple" question, the question is can you find those edges, and code around/through/handling them. In an interview if they have limited time, they'll usually move you towards the simple idea and say you should start implimenting but I always think it's better to understand the problem before tackling it.
Real world problems (aka something your job gives you to do). I attempt reproduce the problem before anything. If you can't reproduce it, you can't fix it. Simple as that (ok some execptions but not really, you know?) So you need to understand the problem. Also "Why was this bug or issue created". Sometimes people are stupid and don't test their code, most of the time someone was trying to solve something else and left a bug. Sometimes "People weren't expecting this specific configuration." Until you understand why the code is in the state it's in it's hard to solve.
Then you really think about what you're trying to solve. Are you trying to add functionality, are you fixing a bug, are you just patching it so the system doesn't crash. Fix the smallest issue if possible, though if you see larger ones, raise that up the flag pole.
Compiling problem. Google... google google. google, oh look there's the answer, pray it's the only compile is you'll have.
50
u/CorrSurfer May 18 '20
It depends on what kinds of problems you are solving. Here a few sentences for algorithmic problems:
- Stare at the problem for a long time. Filter all thought that you are getting for whether they make sense for this problem. Evaluate and re-evaluate.
- Find a reason for why the problem at hand should be NP-hard, PSPACE-Hard, polynomial-time, whatever
- Google for keywords that you believe to be relevant for similar problems. Look at the research papers, read the introduction.
- Think about individual cases of your problem. How would you solve it by hand? What results are you expecting? How would you verify the results to be correct?
- Think about a brute-force solution. Can you speed it up by reduction to more classical problems?
- Explain the problem to a peer. Listening to the reply is best, but explaining is already good.
- Think about how you would pitch the problem as an interesting one. What is the unique property that makes it challenging? If the problem would only consist of this property, how would you solve it? Can you use this solution and add the details as you go?
150
u/monyouhoopz May 18 '20 edited May 18 '20
19
u/brett96 May 18 '20
The first thing you do when starting a project isn't to start coding, but rather start planning. Have an overall structure for your project in mind, know what the end goal is and how you plan on getting there, and once you have a good understanding of what the problem is, then start programming.
It can also be helpful to consider invariants, or things that will always be true about your program. For example, if you were coding a BST, an invariant would be that the value of a node's left child will always be less than the parent node.
These can help with logic errors that come up
7
u/[deleted] May 18 '20
Barbara Oakley's book on learning to solve problems might be a good fit for you.
7
u/blazing_shuffle May 18 '20
I use Problem - Given - Use - Solution
Problem: have a well defined problem.
Given: gather information.
Use: what resources do you have?
Solution: Make a solution and measure it against the problem.
This works for Googling as well because you can "use" the internet :)
7
u/JustinsWorking May 18 '20
I always break it down:
I try to establish clearly what the goal is and what the starting state is.
Generally the language when somebody requests a change or a bug is very vague; trying to clarify the request into very simple precise language helps to prevent fixing the wrong thing or missing edge cases.
Once you have an obnoxiously clear idea of the current state and the goal state, you need to break down how you get from one the other.
I like to map the tasks it out on a whiteboard with arrows, I generally do it a few times as I start to work out a mental model of how the problem flows and how it is structured. This is when I’ll start to see patterns, possibly how it relates to other systems or tools.
Once I’ve done this I tend to build up a task list of things I need to do to make it work, and how things should look at the end of those task. If you’re working in a really clean codebase with good testing tools you can start to write unit tests here; if you’re working on a mere mortal code base you can start to block in code with comments or even possible hard coded values at certain steps.
Then I start to flesh out my plan and implement it.
If I run into issues where I can’t do something I thought I could, or the was external effects I didn’t predict/know about, then I go back and see if there is a different route or how my new knowledge might change the way I approach the problem.
Basically just repeat that till it works.
Once you have the plan on a whiteboard, it’s also a good time to bring in technical help if you’re unsure; I would help identify potential issues or pose different solutions to problems at that point when I was working with junior programmers; if I was building something complicated I’d usually bring in a maintainer of that project and ask them to just take a sniff and see if they spotted any misunderstandings or gotchas I might have missed due to my lack of knowledge/familiarity with any one feature or system.
Hope that helped, I basically chug through this system like a robot day in and day out so it works quite well for me.
6
u/GayMakeAndModel May 18 '20
One thing I do NOT do is make a tweak to fix something without fully understanding what the problem is in context. It’s sometimes very tempting to be like, “Oh, I don’t know why that’s NULL, but a NULL check should do it to get the release out the door.”
Every time I have done this, it ultimately caused more stress than the original problem.
12
u/Jesus-face May 18 '20
Flow chart:
3
u/gingenhagen May 18 '20
How to Solve It is one of the classic texts on the topic.
3
u/Average_Manners May 18 '20 edited May 18 '20
I prefer the top-down approach.
Visualize the end goal.
Find the intermediary goals.
Break the problem down into smaller and smaller pieces until you have the smallest bits of logic you can get. i.e. Abstract.
[Optional, but recommended] Make the entire thing into a flow chart.
Make tests that ensure each function will do what it's supposed to.
Get to writing. Tackle the ugliest block first. The black box which makes you scared is where the demon, who decides whether your approach works or not, lies.
E: Forgot the last steps.
After finishing, reflect. Even great programmers have places to grow.
Clean-up and improve until satisfied.
3
u/whatsooever May 18 '20
Some of you have already mentioned the top-down or given-use approach, but another step I do is to draw it out in the form of flowchart. It’s so helpful to see a visual layout of the code. It also helps for record, future update (because god knows I forget what I code a few days later), or when you try to explain your code to someone, or when someone try to understand your code without you being there. Draw.io is the best chart creator I know. Just draw it on paper while you’re working then redo it on draw.io at the end if you want a pretty chart.
3
u/jagdishjadeja May 19 '20
DIVIDE AND CONQUER!
3
u/Madsy9 May 19 '20
I prefer Feynman's algorithm:
2
u/FUZxxl May 18 '20
The basic idea is to know many standard problems and recognise when you can model the problem at hand as a combination of those standard problems.
2
u/shaggorama May 19 '20
Polya's method.
https://en.wikipedia.org/wiki/How_to_Solve_It
3
u/[deleted] May 18 '20
i hope you did not assume problem solving is only for programming in computer science. i was ready to answer differently until i read 'experienced programmers'. even then the question is vague.
2
u/fr0stheese May 18 '20
Interesting, I will watch this for advice, but I doubt that there's a general method for solving every problems, this would be too easy
1
u/cold_lights May 18 '20
Remove all the possible variables and reattempt
1
u/lVlulcan May 18 '20
As someone with lesser coding experience (just finished my first year of college), THE DEBUGGER. It has saved me hours of isolating issues and the amount of people in my class/others that can’t use it even though it was taught to us is astounding
1
u/[deleted] May 19 '20
One thing that helps me mostly is to remove myself from the environment where I am facing the problem.
Solving some issue or integrating something but it looks impossible? Take a break, drink tea with your mates.
I have found that most of my sudden aha moments happen after this.
1
u/moses_the_red May 19 '20
What happens far more often is that I have multiple solutions with pros and cons. I have to choose between two or more approaches that each have their own tradeoffs.
My amazing technique for dealing with this is writing a little essay about it. Write down how both candidates (or more if you have more than one candidate) work, then advocate for that technique in a section, then criticize that technique.
I find that if I don't take the time to explicitly write down a criticism of the technique I'm using, I run the risk of using that technique for the wrong reasons. Sometimes you start writing one of these essays sure that its going to be the technique you're going to use, and then you hit the criticisms section and you realize that you really just wanted to do it because it was cool or clever or interesting, not because it adds value to the product.
I find that this tends to help me avoid my own biases (we all have them). Helps me avoid stupid things like using a more powerful abstraction than is necessary.
That is probably not what you were really looking for when you posted this though. I imagine what you were really looking for is this:
https://en.wikipedia.org/wiki/How_to_Solve_It
1
u/mcspooky May 19 '20
Brute force
1
u/gitcommitshow May 19 '20
10 techniques to solve problems as mindful developer
1
u/HandsomeBronzillian May 19 '20
Prostpone