I tend to treat programming exercises like bosses in a Soulslike game: I keep trying until I finally beat the problem. The trouble is that some exercises can consume an hour or two—or even longer—without me finding a solution. Is it reasonable to look at the answer after being stuck for that long, or should I instead look up individual functions and techniques related to the specific part that isn't working?
Seeing a solution often gives me an immediate "ah, now I understand" reaction, but I'm unsure whether that feeling represents real learning. I also don't want to develop a habit of searching for the answer every time I get stuck. What's a good way to use solutions, hints, or other resources without undermining the purpose of the exercise?
4 Answers
Don’t underestimate taking a break. After making a serious attempt, step away physically—go for a walk, eat, or do something unrelated. Your mind may continue processing the problem in the background, and returning later can make the missing idea easier to see. A break is often more productive than spending several more frustrated hours staring at the same code.
Use hints before revealing the entire answer. Ask what concept, data structure, or prerequisite you might be missing, or focus on the specific behavior that isn’t working. Debugging with small experiments and print statements can also show you what the program is actually doing. The goal is to get enough guidance to make the next step yourself, not to have someone complete the exercise for you.
Looking at a solution isn’t automatically harmful; copying it without understanding is the real problem. Try to study the approach, close the reference, and then recreate it yourself. A useful test is to attempt the same exercise again the next day without help. If you can explain and reproduce the solution, you probably learned something. If not, review the underlying idea and try again rather than simply copying more code.
There’s no universal time limit, but spending hours on every exercise may not be the best use of practice time. If you’ve made a genuine attempt and are no longer making progress, look at a small hint or the relevant technique, then solve a similar problem yourself. Repeated exposure to many exercises helps build intuition about which algorithms and data structures fit a problem, while getting permanently stuck on one exercise does not.

A helpful progression is to ask for a broad hint first, then a more specific hint, and only look at the full solution as a last resort. Afterward, rewrite the solution from memory and compare your approach with the reference.