• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A Programming Interview Question

 
mark ralphael
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a class called GenNumbers which will generate the following output to the console (use the standard Java, System class). Implement a standard main method to generate the following output.

1
22
333
4444
55555
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
mark ralphael
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc, thanks for the answer.
I was unable to resolve this trivial question, can you give me any advice on how i should go about solving the problem. I wouldn't of thought of doing a nested loop ???

Your advice is much appreciated.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't he just give you the answer? What more advice do you need?

Layne
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nested loop? Heck, you can do it with just a single loop:



- Jeff
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mark ralphael:
Hi Marc, thanks for the answer.
I was unable to resolve this trivial question, can you give me any advice on how i should go about solving the problem. I wouldn't of thought of doing a nested loop ???


I know your question was directed at Marc, but I'll put my two cents in. Mostly so you don't think I'm picking on you by posting my horrible code examples.

Like with most things in life, most of the trick of getting the solution is in posing the question in the right way. For example, what if I had given you the exact same problem, but put it this way:

Write a Java program to print out five lines.

On each of these lines, have it print the same number of characters as the line number. That is, for line one, print one character. For line two, two characters, and so on.

(Oh, and by the way... the character I want to you print should match the line number.)


You'd have probably been able to come up with the solution pretty quickly, wouldn't you? That just screams out "nested loop". You have to have a loop to print each line, and you'll need another loop to print out the correct number of characters.

So I guess it's a matter of recognizing the pattern, and then translating that into the programming idiom. The more you program, the better and quicker you get at doing that.

Hope that helps.

- Jeff
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Jeff illustrated, it's a matter of interpreting the problem conceptually before you get lost in the code details. Before you start driving, sketch out a map. If you take a step back and look at where you want to go, you'll see...
  • You want to print out 5 lines. Okay, so that's a loop.
  • Now, within each of those lines, you want to print out a certain number of characters. Okay, so within each iteration of the first loop, we have another loop.
  • The number of inner loop iterations depends on the outer loop iteration. In other words, the upper limit of the inner loop's counter (y) is a function of the outer loop's counter (x).
  • (By the way, that binary approach is pretty clever. )
     
    Steven Bell
    Ranch Hand
    Posts: 1071
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another thing to consider is what is known as the inventors paradox.

    When I see that question I don't see print:
    1
    22
    333
    4444
    55555

    I see print X number of lines where each line will have X number of characters of the value X.
     
    Carol Enderlin
    drifter
    Ranch Hand
    Posts: 1364
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Alternatively, XP says YAGNI ... you aren't gonna need it.


    [ April 01, 2005: Message edited by: Carol Enderlin ]
     
    mark ralphael
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much Steve, Marc, and Jeff. Your input has been extremely helpful. I hope to speek to you again in the future.
     
    Jeff Jetton
    Ranch Hand
    Posts: 71
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by marc weber:
    (By the way, that binary approach is pretty clever. )


    Thanks! I was pretty psyched to come up with it myself.

    But it does serve to further flesh out what we're talking about: One you recognize a pattern, the programming is a lot simpler. My alternate solution just comes from seeing a second pattern in the example output:

    1 * 1
    2 * 11
    3 * 111
    4 * 1111
    5 * 11111

    It's more of a mathematical way of looking at it than a procedural way, but once you spot that pattern, and notice that the second number looks suspiciously like the decimal interpretaion of a binary number, the "one loop" version becomes a no-brainer.

    - Jeff
    [ April 02, 2005: Message edited by: Jeff Jetton ]
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another tip...

    When coding a loop, remember that the first and sometimes second iterations are often special cases. This is because doing something 0 times or even 1 time doesn't doesn't give you a sense of "iterating." In other words, these are not good models for all the other iterations that will follow.

    So when coding a loop, you should mentally put yourself "in" a few steps to get a better feel for the loop's typical behavior.

    Specifically, in this example, printing the character "1" a single time might not give you a sense that you actually need to count the number of times you're printing.
     
    Stan James
    (instanceof Sidekick)
    Ranch Hand
    Posts: 8791
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just for grins, you could give yourself a handful of additional excercises to play with. Draw a symmetrical triangle, a diamond, an empty diamond, a zig-zag line. Some times you'll get something working then read it carefully and realize you can remove half of it. That's when you start building your skills through these tiny problems.
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's a popular one...
     
    Dhar Desai
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is fun:

    String s ="1";
    for(int i = 1; i <= 5; i++){
    System.out.println(s.replace('1', ("" + i).charAt(0)));
    s +="1";
    }
     
    Ryan McGuire
    Ranch Hand
    Posts: 1068
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Now if the problem had been to print out the first five rows of Pascal's Triagle (that's the right name, isn't it?), I would have been really psyched.

    1
    11
    121
    1331
    14641


    Ryan
    [ April 03, 2005: Message edited by: Ryan McGuire ]
     
    Dhar Desai
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's recursive version:

    public static void genNumber(int i ,int j)
    {
    int k = (i*10) + 1;
    System.out.println(k*j);
    if(i == 1111) return;
    genNumber(k, ++j); //NOT j++
    }

    call genNumber(0,1) to start the whole thing.
     
    Ravi Srinivas
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How abt this


    [ April 04, 2005: Message edited by: Ravi Srinivas ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic