CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 69

Thread: Fun C++ Problem

Hybrid View

  1. #1
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Fun C++ Problem

    Hey guys!

    This is not a homework assignment or anything.

    A buddy of mine who works for a software company in Michigan sent me this problem.

    He said it was on their yearly technical tests that they give programmers.

    I only could figure out two ways to do this... and that took me a LONG time.

    As far as the third solution goes, I have no idea, and neither does my friend. I can give you the first two though later on.

    take the following code:

    int i, n = 20;
    for (i=0; i<n; i--)
    {
    cout << "x" << endl;
    }

    by changing only ONE character in the above code, meaning you cannot change 20 to 31, because you will have changed two characters, you can change 20 to 21, because you only changed the 0, do the following:

    find 3 ways to make the above code print x 20 times (by changing only one character).
    And, according to my friend, he said his boss has been giving this problem yearly since about 1999, and no one has figured out all three ways to do it.

    Good luck, this drove me nuts for a while!
    Last edited by dcjr84; October 18th, 2006 at 12:18 AM.
    Please rate my post if you felt it was helpful

  2. #2
    Join Date
    Aug 2005
    Posts
    478

    Re: Fun C++ Problem

    I can get two also but a 3rd is hard! I'd be fairly certain that we are probably stumped on the same thing now.
    Windows XP, Visual Studio 2008, SVN

  3. #3
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Fun C++ Problem

    Don't post any solutions yet if you have them.

    I want to see how many people can actually figure out all three ways , but not until everyone has had a chance to read the problem.

    Plus, it's fun to watch people "sweat" a little over a fun problem like this
    Please rate my post if you felt it was helpful

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  5. #5
    Join Date
    Aug 2006
    Posts
    16

    Re: Fun C++ Problem

    Can you insert a character? or is it only replace.

  6. #6
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Fun C++ Problem

    Can you insert a character? or is it only replace.
    You can only change (replace ) existing characters in the code.

    Nothing can be added.
    Please rate my post if you felt it was helpful

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Fun C++ Problem

    This is one of the trick questions a friend of mine was asked in the interview for his job at our current companies. I remember that I needed 2 minutes to find the first two, and more than 10 minutes to find the 3rd. It's a good one!
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Fun C++ Problem

    Quote Originally Posted by *io*
    Can you insert a character? or is it only replace.
    Well, I guess replacing a whitespace is inserting and since you want to insert - insert where there is a whitespace, it is replacing not inserting

  9. #9
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Fun C++ Problem

    Quote Originally Posted by exterminator
    Well, I guess replacing a whitespace is inserting and since you want to insert - insert where there is a whitespace, it is replacing not inserting
    Ouch, my head hurts.

    Reminds me of "how much wood could a woodchuck chuck if a woodchuck could chuck wood?"
    Please rate my post if you felt it was helpful

  10. #10
    Join Date
    Oct 2006
    Posts
    1

    Angry Re: Fun C++ Problem

    Got two in a few minutes. But the third one took me 30 minutes!!!

  11. #11
    Join Date
    Oct 2006
    Posts
    2

    Red face Re: Fun C++ Problem

    Hi,

    A nice one, it took me about 3 minutes to get first solution, then it was easy, I could get all the three in 5 mintes.

    How is it going for others?

  12. #12
    Join Date
    Aug 2003
    Location
    Canada
    Posts
    164

    Re: Fun C++ Problem

    I got all three of them in a few minutes, because I thought about the loop terminating with an answer that is not Boolean.

    I really need to point out that such a puzzle is a bad interview question. Such puzzles are fun, but they are not a good indication of programming ability. More importantly, as Joel Spolsky says it, it doesn't show if the candidate is smart and gets things done. I highly recommend his article:

    The Guerrilla Guide to Interviewing
    Finally, avoid brain teaser questions like the one where you have to arrange 6 equal length matches to make exactly 4 identical perfect triangles. If it's an "aha!" question, you don't get any information about "smart/get things done" by figuring out if they happen to make the mental leap or not.
    A great read. The puzzles are fun, no doubt. I love them. Just don't judge people based on how quickly they can solve it...

  13. #13
    Join Date
    Feb 2005
    Location
    Denver
    Posts
    353

    Re: Fun C++ Problem

    Quote Originally Posted by JasonD
    I really need to point out that such a puzzle is a bad interview question. Such puzzles are fun, but they are not a good indication of programming ability. More importantly, as Joel Spolsky says it, it doesn't show if the candidate is smart and gets things done.

    The puzzles are fun, no doubt. I love them. Just don't judge people based on how quickly they can solve it...
    Well put. I couldn't agree with you more.

  14. #14
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Fun C++ Problem

    Yes, it's a strange interview question.
    My hobby projects:
    www.rclsoftware.org.uk

  15. #15
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Fun C++ Problem

    I found the first solution in the first minute, then I thought 'what a pointless exercise' and went off and did something more interesting instead.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured