Printing a multiplication table
Suggested interviewing technique

When writing code for an interview, always ask questions to seek clarification from your interviewer.  There are nearly always technical decisions to be made, and you want to make choices that match what they have in mind.  (If you don't have any questions, then you probably haven't thought things through properly.)

For this task, I would ask:


"Can I assume that the the input x is a reasonably small positive integer, not requiring validation?  It doesn't make much sense to print anything larger than 100 × 100 anyway, right?"
"I assume that you want the entries to be aligned nicely in a grid?"
"I assume that we don't need row and column headings, and that we don't need to print grid lines?"
"I assume that you want a reasonable balance of simplicity and speed?"


Before you even write a single line of code, you can score points by showing that you know how to negotiate software requirements.

Here's a solution that I would have written:

public static void printMultTable(int x) {
    int width = (int)Math.floor(Math.log10(x * x)) + 2;
    String fmt = "%" + width + "d";
    StringBuilder b = new StringBuilder(x * width);
    for (int row = 1; row &lt;= x; row++) {
        b.setLength(0);
        for (int col = 1; col &lt;= x; col++) {
            b.append(String.format(fmt, row * col));
        }
        System.out.println(b);
    }
}


After writing it, explain and defend any technical decisions:


"To achieve grid alignment, we need to first determine the width of each entry.  String.valueOf(x * x).length() + 1 would have worked, but I think that the mathematical approach is slightly more efficient."
"I could call System.out.print() for every entry, but printing an entire row at once should have less overhead.  Printing the entire table at once might be even faster, but I figured this was good enough."


Note that other answers are possible.  Your interviewer might have answered, "I don't care about any of those requirements.  I just want to know that you know how to write a nested loop."  In that case, just write the simplest code that prints out the right numbers with sloppy formatting.

Your solution and feedback

My biggest concern is that it is not obvious from inspection what your code does.  There are a lot of variables, and the flow of control is convoluted.


I think number is superfluous.  After each assignment, you use it just once.
Why is rowCounter initialized to 0, but columnCounter initialized to 1?  Why is there a rowCounter = 2 assignment right in the middle of everything?  (I would expect reinitializing to either 0 or 1.)  More alarmingly, you increment rowCounter to count the columns printed, and columnCounter to number the current row.  That's the kind of trap that makes co-workers hate your code.
Three cases is excessive.  All of them have System.out.print(… + " "), which makes it cut-and-paste code.  Couldn't you lump the first if(i &lt; x) case with the last case?  (Hint: setting rowCounter = 0 was a poor move.)


The natural way to write this solution is using a nested for-loop.  If you really wanted to do it in one loop, it should be simpler:

public static void printMultTable(int x)
{
    int row = 1, col = 1;
    int totalEntries = x * x;
    for (int i = 1; i &lt;= totalEntries; i++)
    {
        System.out.print((row * col++) + " ");
        if (i % x == 0)
        {
            System.out.println();
            row++;
            col = 1;
        }
    }
}


Note that the function should be static, since there's no reason for it to have any object state.


  I’d say that a lot of his solutions were pretty inefficient and took a very ‘brute force’ approach to a solution. For example, his solution for question 1 took 30 lines (give or take for some spacing). This is normally done in about 8 lines.  I did like that he documented all of his answers very well, so that’s a positive, …


Comparing your solution against the others posted here, I think you would agree that the logic was unnecessarily convoluted.


  but yeah, just a lot of inefficiency throughout his answers (processing inefficiency, object allocation inefficiency, etc)


For all practical purposes, there isn't going to be much user-visible difference in performance of your printMultTable() and the better implementations.  I don't think that there's much object allocation to speak of in this particular solution; that feedback probably referred to the solutions you wrote for the other tasks.  But judging from the way you used a zoo of variables without any clear purpose, I'm inclined to believe their assessment.