Thread: interview question

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    KOLKATA, INDIA
    Posts
    3

    Lightbulb interview question

    One of my friend was asked in his interview to WRITE A ONE LINE PROGRAM TO ACCEPT A NUMBER FROM USER AND PRINT THE SAME. The main condition is to write the program in one line(i.e., only in one statement)only. Is there any way to write a program in that manner. If yes, then pls show me the code in reply.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    One line? In C? Write program, remove line returns. Done. The C compiler does not care about how many lines there are, just the semicolons. One statement? It's (virtually) impossible to write a valid C program to do the same without more than one semicolon (and certainly not portably), so no. You can do some horrible macro trick most probably but that's really just cheating about the definition "statement".

    If they didn't specify the language, in BASIC it's even easier, and in things like Perl it's a cinch if you know the language.

    That said, it's a fecking stupid question that should never have been asked. You do *NOT* want to work for the person who asked that, and I'd have told them so in the interview. Something like "Sorry, but I only write good, well-documented code for real-world problems, not unnecessarily obfuscated logic tricks and trivia that would be non-portable and impossible for others to continue my work on." Having "odd" interview questions is about determining how you think, not what answer you give, but some interviewers have missed the point and think that asking the same questions as Google is "good interview technique". It's not. What you want to know is *HOW* they would go about solving a seemingly-impossible problem, not what their answer is, or even if they could find an answer. And guess what answer a person in charge of a team of professional programmers would want to hear about such a stupid, artificial restriction that could only result in unreadable, unportable code? "Sorry, but I only write..."

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I could do something like that with a single statement in C++.
    But am not sure how portable it is..or whether the same can be done in C.
    The statement is:
    Code:
    std::cout<<std::cin.rdbuf();

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by manasij7479 View Post
    Code:
    std::cout<<std::cin.rdbuf();
    Except that's not a valid *program*. For a start, there's no entrypoint, no definition of std (because you haven't included iostream), etc. etc.
    There's a vast difference between a program and a statement. The OP said a one-line program.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It's not hard to do in C or C++, as long as you keep in mind that declarations are not statements. There are several ways to write single statements with multiple effects. And the easiest way, in C or C++, to obtain user input is from the command line

    However, I agree with ledow. If I was asked such a question in an interview, I would ask if they expect such code to be written in a production environment. If they do expect such code, or don't actually answer my question, then I would apologise for wasting their time and end the interview.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by ledow View Post
    There's a vast difference between a program and a statement.
    And the first post included the phrase : "(i.e., only in one statement)"
    Do verify your points before jumping up to nitpick.

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    As well as saying:

    "WRITE A ONE LINE PROGRAM" and "The main condition is to write the program in one line"

    But who cares. The statement is easy (because you can just assume that all headers and boilerplate are included), the program is all-but impossible without some horrendously obfuscated macro tricks, I imagine.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interview Question
    By nndhawan in forum C Programming
    Replies: 42
    Last Post: 02-18-2011, 02:49 PM
  2. Interview Question
    By kiros88 in forum C Programming
    Replies: 11
    Last Post: 10-02-2010, 08:34 AM
  3. Interview question
    By vijay s in forum C Programming
    Replies: 6
    Last Post: 04-29-2010, 01:40 PM
  4. An interview question
    By kris.c in forum C Programming
    Replies: 3
    Last Post: 05-18-2007, 07:33 AM
  5. An interview question
    By kris.c in forum Tech Board
    Replies: 10
    Last Post: 01-10-2007, 02:58 PM