How to develop logic or approach for solving a question?
As a programmer in your initial days, you might encounter a common and reasonable problem: struggling to come up with code solutions while practicing coding questions and working with data structures. Despite knowing the syntax of programming languages well, you may find yourself unable to crack certain problems, leading to frustration and demotivation.
Fear not! We’ll explore an approach that can transform your problem-solving experience. Instead of viewing coding questions as mere technical exercises, let’s treat them as riddles waiting to be solved. Just like solving a riddle, we’ll engage our minds and build step-by-step solutions. Here’s how you can approach it:
1. Think of Questions as Riddles
When faced with a coding question, shift your perspective. Imagine it as a riddle—a puzzle waiting to be unraveled. Riddles often require creative thinking, lateral reasoning, and a dash of intuition. Similarly, approach coding questions with curiosity and an open mind.
2. Solve on Paper
3. The Power of Paper
Why solve on paper? Here are some advantages:
Remember, “A piece of paper has been a best friend to humans even before computers existed.” Leverage this timeless tool to your advantage.
4. Transition to Code
Now that you’ve cracked the riddle on paper, it’s time to translate it into code:
To summarize this article, these are the steps you can follow:
An example:
One example of how you can tackle such questions:
Recommended by LinkedIn
Let us say we have to find whether a number is prime number where the number n is taken as input.
Approach to build:
Here you will check if the number is prime or not. Think of it as you are finding a prime number in your math question. How do you say is a number is prime or not. You say that if a number is divisible by 1 and itself than the number is prime, otherwise not.
A Python Example:
Now see this code explaining itself:
#python program
n=int(input())
ans=1
for i in range(2,n//2+1): # checking if the number is divisible by any other number smaller than it
if(n%i==0):
ans=0
break
if(ans==1):
print("number is Prime")
else:
print("number is not Prime")
In the for loop we check all the numbers smaller than its half are factors of the number or not. And using this logic we could find the question.
Some important points to remember:
The Art of Problem Solving
Sometimes arriving at a solution can be messy, but writing it down on paper helps clarify your thoughts. Break the problem into smaller steps, just like you would in a math exam.
Master Data Structures and Algorithms
Invest time in learning data structures and algorithms. They not only optimize your code in terms of time complexity but also provide tools to tackle various problem types. A solid understanding of programming languages is equally crucial.
Practice, Practice, Practice
Remember, practice makes perfect. Regular coding practice sharpens your skills, builds confidence, and helps you improve. Keep pushing forward, adapt, and evolve.
For this you can use platforms like LeetCode , GeeksforGeeks , CodeChef , Coding Ninjas etc.
Conclusion
Next time you face a coding question, remember this process. Treat it as a riddle, solve it on paper, and then code it up. With practice, you’ll find that the transition from riddles to code becomes smoother. Happy coding!
Feel free to adapt and expand upon this article as needed. Happy problem-solving! 🚀📝
Thank you for reading this.