aspose file tools*
The moose likes Java in General and the fly likes A Programming  Interview Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "A Programming  Interview Question" Watch "A Programming  Interview Question" New topic
Author

A Programming Interview Question

mark ralphael
Greenhorn

Joined: Mar 29, 2005
Posts: 17
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

Joined: Aug 31, 2004
Posts: 11343



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
mark ralphael
Greenhorn

Joined: Mar 29, 2005
Posts: 17
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

Joined: Dec 06, 2001
Posts: 3061
Didn't he just give you the answer? What more advice do you need?

Layne


Java API Documentation
The Java Tutorial
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
Nested loop? Heck, you can do it with just a single loop:



- Jeff
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
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

Joined: Aug 31, 2004
Posts: 11343

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

    Joined: Dec 29, 2004
    Posts: 1071
    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

    Joined: Oct 10, 2000
    Posts: 1364
    Alternatively, XP says YAGNI ... you aren't gonna need it.


    [ April 01, 2005: Message edited by: Carol Enderlin ]
    mark ralphael
    Greenhorn

    Joined: Mar 29, 2005
    Posts: 17
    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

    Joined: Mar 29, 2005
    Posts: 71
    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

    Joined: Aug 31, 2004
    Posts: 11343

    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

    Joined: Jan 29, 2003
    Posts: 8791
    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.


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Here's a popular one...
    Dhar Desai
    Greenhorn

    Joined: Nov 01, 2004
    Posts: 11
    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

    Joined: Feb 18, 2005
    Posts: 1013
        
        3


    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

    Joined: Nov 01, 2004
    Posts: 11
    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

    Joined: Sep 04, 2003
    Posts: 26
    How abt this


    [ April 04, 2005: Message edited by: Ravi Srinivas ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: A Programming Interview Question