Firstly read the problem statement carefully, then segregate your problem. Is the problem related to sorting, searching, patterns, segmented sieve, strings, arrays, binary bits, etc. For example- If problem statements ask about binary search on rotated array then it is related to two types of categories i.e. searching and arrays. So, your code will be according to these two categories only.
In every problem statement there is a range limit given till which the test cases will be tested. As these range limits are very high (around 108) so you should declare a variable which can be used afterwards in the code. This makes your code simple.
For example- If your limits are given as
0 ≤ n ≤ 106
Code: #define ll long
While solving a problem divide your code into smaller units which makes your code simple to use and reduces the time complexity, which in turn reduces the time limit error. For example- If the problem expects more from your code such as sorting of strings in lexicographic and numeric type then you needs divide it into multiple segments. Segments like lexico compare, numeric compare, string to integer convertor, string extraction etc.
Declare all the variables at the start of the code. This reduce the occurrence of time limit error (TLE) in the code and makes your code simple. Time limit error TLE comes because the online judge has some restriction that it will not allow to process the instruction after certain Time limit given by Problem setter usually. So declaring your variables at start reduces the total execution time. For example- If your problem is about getting prime number in between two numbers you need to declare an array of size given in the program. And if you declare variables afterwards it may give you a TLE which reduces in pre-declaration variables.
While coding you may have faced losing time in coding for all the functions. But by knowing many languages you can use different in-built functions present in different languages. Also in competitive coding competitions you are given different time limits for differ languages so you can write in a language with maximum time, which will reduce your TLE. Also execution time of some data structures like dictionaries, list present in python.
Visualizing your code helps you get an idea about how your code works.
You can visualize all your codes here(independent of language).