Skip to main content
7 answers
7
Asked 1842 views

How can I improve my algorithm thinking?

I struggle trying to create simple algorithms. For instance, I would spend almost an entire day trying to figure out how to print an inverse right triangle with asterisks in my computer programming class while my fellow peers would solve it in 30 mins. #computer-science #programming #java #critical-thinking

Thank you comment icon Hey Paul! I am a second year computer science major in college, and I often struggle with making algorithms as well. Something you can do is use online resources such as hackerrank.com and leetcode.com to practice algorithms. The "easy" questions may be difficult at first, but make sure to take your time and really think about the problem and the different ways you can solve it. If you happen to get it, great! If not, don't feel bad about looking at the solutions, just make sure you understand them. Hope this helps! Albert

+25 Karma if successful
From: You
To: Friend
Subject: Career question for you

7

7 answers


1
Updated
Share a link to this answer
Share a link to this answer

Daniel’s Answer

If it's truly algorithms you're struggling with, then two things come to mind:


1) Study more math. Algorithms is inherently more of math problem than a programming problem.


2) Get a good algorithms book and learn from it (e.g. CLRS is the canonical textbook).


On the other hand, if it's basic programming and not the algorithms that trips you up, then the best advice is to practice more, and learn how to break things up into tiny pieces. So for printing a triangle, my thought pattern would go something like:


1) can I print anything to console? If not, Google how to print stuff in whatever language
2) run a basic program printing one line with at least a couple of just to see it work.
3) do I know how to concatenate strings or print single character? If not, Google how to print single character or add strings together
4) figure out how many spaces and
to print on a given line (should probably be some math formula, see above)
5) try a lot of for loops

1
0
Updated
Share a link to this answer
Share a link to this answer

mathias’s Answer

This is comparable to writing an essay or a short story. If the teacher says, "write something" people tend to spend a lot of time before starting to write. But the more specific the task is, like "write something about the face side of a quarter", the more likely people will start writing immediately.

A key here is breaking the problem down into manageable pieces that you clearly know what to solve.

e.g. First step, print a single asterix on the screen -- basic, but your first victory
Second step, make a loop to print 10 asterixes
Third step ...

As you complete each step you only have to formulate a new task to take you closer to the final goal. You don't have to solve all remaning items in the next iteration.

The more you work like this, the more it will become automated so that a year you'll pack each "small" step with lot's of single steps that initially were too complex to bundle at the time.
0
0
Updated
Share a link to this answer
Share a link to this answer

AKSHAY’s Answer

Hello Paul,

As per my experience i divide this into 2 category:
1. Focusing on understanding the ideas very deeply(Depth Approach)
To start with this you need to focus on a few representative problems lets say 100 to 200, Solve them a couple of times and you’ll start seeing
patterns. You also start getting better at the coding part. Once you achieve this you will be able to :
> You can code the usecase quickly.
> You can apply the same code to a new problem quickly .
> You know the data structure you are using and can implement it .
Once you are done with this pls revise the core sets of algorithm i strongly recommend "Cracking the Coding Interview".

2. Focusing on solving as many problem as you can (Breadth Approach)
Here you can now look beyond your core set of problems. Because you’ve implemented so many techniques already and gone through core
sets of algorithm, you don’t even have to code all the new questions.BUT Practice the sudo code on PAPER if you do so you are forced to plan
your code before writing!.

-
Akshay

AKSHAY recommends the following next steps:

Isolate your Techniques like Depth First Search Recursion + Memoization Hash Table + Linked List combination Searching a Binary Tree etc.
Familiar with Core Data Structure : Arrays and Lists 2D Arrays Strings Linked List Stack Queue Hash Table & Hash Set Heap Graphs Binary Tree Binary Search Tree Trie.
0
0
Updated
Share a link to this answer
Share a link to this answer

Earl A.’s Answer

Hi Paul,


Programming is often a "divide and conquer" task. You usually can't jump from the problem to finished code in one step.


Write the steps of the problem in English (or your native language) or a pseudo-language, and then translate that into code/language.


Trying to work out the algorithm and the coding in one step can be intimidating at first. Eventually, coding into the language becomes more fluid and natural, but for now, break it down into steps.


But first, make certain that the problem itself is well defined.


So, for your example, I'm assuming that an "inverse right triangle" is one with a wide, flat side up and the acute angles pointing right and down. (I would call that "inverted" triangle.) I'm assuming that "inverse" means that it should be filled with asterisks, not just an outline. (NOTE: the problem needs to be even more well defined than I have done here! Also, clear up the assumptions!)


But moving on to the next step, write out the steps in your most fluent (human) language:
--- start ---
Set the number of asterisks to the desired length of the first side


Start a loop to print the triangle
Print a string of that number of asterisks
Decrement the number of asterisks
Loop until the number of asterisks = 1
--- end ---


Now, with the problem broken down into discrete steps, you can translate into code.


I hope that gets you headed in the right direction.
Peace, Earl

0
0
Updated
Share a link to this answer
Share a link to this answer

Gautam’s Answer

> Understand the underlying data structures. Once you understand how data is stored, retrieved and maintained internally, you'd have a better intuition of which data structure to use and when.
> Then algorithms require practice and getting yourself familiarized with a wide variety of problems. So, once you start practicing, you will slowly start to come up with better solutions.
> Try to solve a problem in different ways or using different data structures and algorithms and understand the trade-offs between time (speed) and space (memory).
> When you're stuck with a problem, before looking at the solution, try to at least think of a brute-force way of solving the problem. Then think of how you can optimize it.
0
0
Updated
Share a link to this answer
Share a link to this answer

Juwan’s Answer

How about just try to jump on more and more algorithm problems available out there such as https://leetcode.com/.

There are also numerous of various solutions provided by other people for each problems.

0
0
Updated
Share a link to this answer
Share a link to this answer

Sudhansu’s Answer

For any problem solving, one should start will most trivial algorithm (aka brute force method). Overthinking to use complex data structure often hinders problem solving. Hence my simple take is start with simple happy path algorithm (may be with smaller data set), and build on top of that by adding more constraints. Of course there is not substitute to read more and more to build a strong foundation!
0