Writing code is a mentally complex task
While it requires a lot of understanding of the domain in which you are working to solve a complex problem, there are certain ways you can make it easy for yourself and for your brain to process the task and solve it.
Divide and Conquer
This is probably the most important way and efficient way to solve any task and it applies to any problem large or small.
Divide and Conquer means breaking the problem down into smaller tasks until they become easy enough for you to solve. Then solve those individual tasks and you will reach a solution to your actual problem.
It takes time and experience tackling problems every day to do this fast enough or even subconsciously, but that does not mean that you can't do that as a beginner doing that will make you a better engineer over time.
Mindset when solving problems
If the first thing that you do when faced with a task is to start writing code, you might be setting up yourself for long hours of debugging and frustration.
It might seem counterintuitive but even though your solution is in code form, you can usually solve it much better using other methods.
These methods consist of understanding as much of the domain as possible so you know how to position the task. If you approach it with a hammer the task will look like a nail to you, when instead it should be a screw.
Whenever you are faced with a problem, see how it affects everything else.
Write down your assumptions and clarify those first.
Write down the steps you can take to solve this problem, break them down as much as you like.
Then start doing the task.
All of these steps might seem like a lot, and you might be asking why bother or if it's a waste of time.
I used to think the same, but I would get stuck on problems and spend way more time trying to get myself out of that localized mindset to think of anything else.
Once you have created a global set of steps, it becomes really easy to focus on what the exact problem is and what you need to solve.