Six Steps Programming Students Can Take to Solve Coding Problems

As a tutor, I’ve worked with lots of computer science students who are new to programming. One of the issues that I see often is that students have trouble figuring out how to solve programming problems. They’ve done the reading assignments and attended the lectures, but they have difficulty applying what they’ve learned to an actual problem.

Here is a six-step process that I recommend for my students.

  1. Understand the problem statement.
  2. Identify the expected behavior.
  3. Identify the supporting information.
  4. Map the supporting information to the behavior.
  5. Assemble the pieces into a solution.
  6. Test your solution.

Download PDF and Example Code
If you’d rather view this guide as a PDF, I’ve created one for you. I’ve also put together a small sample program, which will be explained in the guide. The PDF and the example code are available for download from:

1. Gumroad (we’ll also be adding more content there as well)
2. Directly from this site

Either link is valid, your choice. Now on to the rest of the explanation.

The Process Explained

1. Understand the Problem Statement
To solve any problem, you must first understand what you’re being asked to do. Usually a coding problem gives you a set of expected inputs and outputs. Look out for extremes and “edge cases” as well; for example, to get an idea of the full range of behavior for a math problem, we might want to look at extremely high and low numbers, negative and positive numbers, and 0.

The problem might also specify that the input or output should be in a specific format, so take note of such requirements.

2. Identify the Expected Behavior
Once you understand the problem, you need to figure out what behavior will give you the expected results. In other words, what set of steps do you need to take in your program to get the correct output from a given input? In computer science terms, these steps are called an “algorithm,” but you can also think of them as a recipe. Think about it: a recipe starts with a set of ingredients (the input), gives you specific steps to follow (the algorithm), and if all goes well, you end up with a delicious dish (the output).

3. Identify the Supporting Information
Often, a coding assignment will also give you additional information aside from the expected outcome. For example, the problem statement might suggest or require that you use certain methods or functions to solve the problem, or it might direct you to some reference material related to the problem.

Sometimes the problem statement will be straightforward and not give any supporting information. In those cases, you still need to know how to do the steps in the algorithm, which might require reviewing your class notes or doing a bit of research. Supporting information can also include information that you already know, if it will help you solve the problem.

Keep in mind that you might need help gathering some of the supporting information. If you’re working through the problem and have questions, you should write them down, and then as you answer each one, you’re adding to your information. Noting specific questions will also make it easier to ask someone for help.

4. Map the Supporting Information to the Behavior
As you gather your supporting information, you’ll notice that it corresponds to specific steps in your expected behavior. Here’s one way to think about it: For each step in the behavior, you’re asking “How do I accomplish this step?” Your supporting information is the answer; it’s telling you “Here is how you accomplish this step.” Making this connection your information and your goal will help you to better understand and write your program. You can even assemble your information and behavior into a table (if you want an example, check the end of this post).

5. Assemble the Pieces into a Solution
Once you have all your information organized and associated with the relevant behavior, you can write your program. By writing out the behavior in order, as in the second step of this process, you’ve already got the idea of how the program should be organized. Now you can put the pieces into actual code.

6. Test Your Solution
The last step is to test the solution you’ve developed. Use the expected inputs and outputs that you identified in Step 1 to verify that your code works.

Hopefully, your program will work correctly at this point, but in reality, most programs aren’t right on the first try. If you notice problems, check for any error messages, and analyze the actual results you get, versus what you were expecting.

The Process in Action: An Example
Let’s try the steps with an example. In this case, I will solve the problem in Java, but you can apply this process to any language.

The Problem
Write a Java program that asks the user for a character and a sentence, and then prints out the number of times that the character is in the sentence.

For example, if the character is ‘a’ and the sentence is “Mary had a little lamb,” the program would print out 4. If the character is ‘a’ and the sentence is “A mad rabbit ate the carrot,” the program would also print out 4. If the sentence is “My dog is cute” and the character is ‘b’ then the program prints out 0.

Step 1: Understand the problem statement.
This problem statement is pretty short, and it gives several examples of expected input and output. We can summarize the expectations in a table:

Input Expected Result
Character: ‘a’
Sentence: “Mary had a little lamb”
Print out 4
Character: ‘a’
Sentence: “A mad rabbit ate the carrot”
Print out 4
Character: ‘b’
Sentence: “My dog is cute”
Print out 0

You can also add your own inputs and outputs to the table, if you’d like more examples you can test when the program is done.

Did you notice anything about the second sentence? The capital ‘A’ was not included in the count. The example the problem gave us tells us that the case of the character matters. Watch out for extra information that the given examples tell you. I would suggest examining each example to see that it makes sense to you, which will help you spot details like this.

Step 2: Identify the expected behavior.
In order to solve this problem, we need to do the following:

  1. Get a sentence and a character from the user.
  2. Create a variable to keep track of the number of occurrences, which starts at 0.
  3. Go through the sentence and check to see if each character is a match for the input.
    • If the character is a match, add 1 to the number of occurrences and move on to the next character.
    • If the character is NOT a match, do nothing to the count and move on to the next character.
  4. Print the number of times that the character occurred.

Step 3: Identify the supporting information.
The problem statement didn’t give us any extra information, but we can see from the behavior that there are several things we need to know how to do. We need to know how to:

  • Get input from the user.
  • Check each character in a sentence, one at a time.
  • Print information to the screen.

Let’s say that from our Java class notes, we know how to print output to the screen (a function called System.out.println(String myOutput)).
The problem doesn’t say how to get the user input, and we can’t seem to find any examples in our class notes, so we need to do a bit of research. We could consult a textbook, or do a Google search (try a search term like “java get char input” or “java get string input”; how to search for information is a broad topic that could merit a separate, in-depth discussion).

After some searching, we find the official online documentation pages for Java, and we learn about the System.console.readline() function, so we add it to our list of supporting information.

We know that we will have to go through the sentence one character at a time. From class, we remember that we can use a for loop to iterate through a string. Looking at the online Java documentation, we also see that Java strings have a method called String.charAt(int index) that returns the character at the specified position in the string, as well as a method called String.length() which returns the length (number of characters) in a string.

We also need to be careful to create and set our counter variable to 0 outside of the for loop. If we set it to 0 inside the loop, each time we check a new character, the counter will be reset to 0, which is not what we want.

Step 4: Map the supporting information to the behavior.
So now we have determined supporting information that will help us to solve the problem, so we can map it to the required behavior.

Step Supporting Information
Get a sentence and character from the user. System.console.readline() gets input from the user.
Create a variable to keep track of the number of occurrences, which starts at 0. Make sure to create and set the initial counter variable outside of the for loop.
Go through the sentence and check to see if each character is a match for the input. A for loop lets us go through the sentence one character at a time.
String.length() will tell our program how many times to run through the for loop.
String.charAt(int index) gives us the character at a given position in the sentence, which we can then compare to the character that the user entered.
Print the number of times that the character occurred. The method System.out.println(String myOutput) allows us to print output to the screen.

Step 5: Assemble the pieces into a solution.
Once you have figured out the behavior and how to implement it, it’s time to put it all together into a complete program. See the accompanying file, CharacterCounter.java, for my solution based on our example. You can open it in a text editor (I like to use Notepad++).

Keep in mind that in many cases, there is more than one way to solve a programming problem, so if you can come up with a different solution, as long as it fits the assignment requirements, it’s probably just as valid.

Step 6: Test your solution.
Ok, so now that we have a program, we need to check that it works. To do so, you can use your list of expected incomes and outcomes from Step 1.

If you have Java installed on your computer, you can test the code yourself (again, the code is available here or here). Even if you’re not familiar with Java, hopefully the code is short enough that you can get a good idea of how it works, now that we’ve walked through our problem-solving process.

Longer Programs
This example was pretty short, but what happens if we have a longer, more complicated program? In such cases, a good approach is to break the program into smaller pieces of functionality, and apply this process to each piece.

For example, suppose we had a similar assignment asking us to find the number of times a character occurs in a text file. We have to do the extra steps of opening and reading a file. We might want to start by figuring out and testing code for how to open a file. Once we have succeeded in opening a file, we could figure out how to read it; we might test that piece by outputting each line to the screen. In this way, we could continue building the smaller pieces into a complete solution.

Conclusion
Hopefully this process helps you to approach programming challenges in a more organized fashion. Feel free to refine this approach to however will best help you. Don’t worry if you don’t get your program right the first time; make careful note of what the actual outcome was versus the expected outcome, and then return to Step 3, adding more information on the error, and asking for help when you need it.

I hope this post and example prove helpful, and feel free to leave comments, suggestions, questions, or other feedback!

JOIN OUR NEWSLETTER
Sign up to receive updates about new tutorials, game news, and educational opportunities.
We hate spam. Your email address will not be sold or shared with anyone else.

Share This:

Tagged , , , , . Bookmark the permalink.

One Response to Six Steps Programming Students Can Take to Solve Coding Problems

  1. Pingback: Stressed and Discouraged Computer Science Students: All is Not Lost! - Cloudy Heaven Games

Leave a Reply

Your email address will not be published.