Solving Programming Problems

Programming problems are not that much different from mathematics or physics problems. There are usually an input and an output to which someone needs to arrive by providing an algorithm. This algorithm is typically a function or series of functions.

Programming puzzles and toy problems are good exercises to sharpen skills and prepare for technical interviews. No wonder that more and more online coding schools (e.g., CodeAcademy) make those metal workouts main staple of their courses.

Beginner programmers might benefit by applying these steps to their process of solving a programming problem:

Programming problems are not that much different from mathematics or physics problems. There are usually an input and an output to which someone needs to arrive by providing an algorithm. This algorithm is typically a function or series of functions.

Programming puzzles and toy problems are good exercises to sharpen skills and prepare for technical interviews. No wonder that more and more online coding schools (e.g., CodeAcademy) make those metal workouts main staple of their courses.

Beginner programmers might benefit by applying these steps to their process of solving a programming problem:

  1. Find the input
  2. Find the output
  3. Come up with some logical algorithm to transform input to output in plain English, a.k.a., pseudo code
  4. Translate pseudo code into programming language, e.g., JavaScript
  5. Run the code mentally (in case of a whiteboarding exercise) or on a machine
  6. Compare current output with the expected output, if they’re not the same rinse and repeat from step 3

Here is an example from Blake Embery’s repository:

Integer Length

Write a function that takes an integer as input and returns the number of digits in that integer.

By applying aforementioned steps, we can come up with this pseudo code:

  1. Get the number
  2. Divide it by 10, if the result is more than 1 that the number is greater that 10
  3. Increment the counter (we now know that we have at least one if the result if greater than 1)
  4. Repeat division with the result as a new number until new result is less than 1
  5. Exit by returning the counter value

Now, let’s translate this into JavaScript/Node.js code:

var num = function (n) {
  var i = 1; 
  while ((n/10)>=1) {
    n=n/10;
    i++
  } 
  return i;
}

To test the functions, you can just run:

num (1000);
num (100);
num (10);
num (1);
Solving Programming Problems
Solving Programming Problems
var num = function (n) {
  // There is an extra check here to ensure the number is an integer
  return ('' + (n|0)).length;
};

In both cases the problem is solved, but how it’s solved differentiate a good programmer from a great one (knowing API is a plus).

Author: Azat

Techies, entrepreneur, 20+ years in tech/IT/software/web development expert: NodeJS, JavaScript, MongoDB, Ruby on Rails, PHP, SQL, HTML, CSS. 500 Startups (batch Fall 2011) alumnus. http://azat.co http://github.com/azat-co

One thought on “Solving Programming Problems”

  1. Interesting article.

    Some years ago my son and I wondered if it would be possible to simply compile the pseudo code and save ourselves a step. You know, think in English and code in English as well. So we decided to take a non-trivial problem (a complete development system including a graphical interface, a file manager, a text editor, a hex dumper, a native-code-generating compiler/linker, and a wysiwyg page editor for documentation) and see if we could write and compile the whole thing in English. Turns out we could. See here:

    http://www.osmosian.com/cal-3040.zip (Windows XP, Vista, 7, or 8 required)

    And it’s less than a megabyte. Just download and unzip; no installation necessary. Start with the “instructions.pdf” in the “documentation” directory and before you go ten pages you’ll be reading the documentation in the built-in page editor and recompiling the thing in itself (in less than three seconds).

    Thanks,

    Gerry Rzeppa
    Grand Negus of the Osmosian Order of Plain English Programmers
    gerry.rzeppa@pobox.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.