60

One of my interview question for fresh graduates was to switch 2 variable values without adding a new variable.

I was expecting something like

a= readline()
b= readline()
a+= b
b= a - b
a-= b

And some kid answered:

a= readline()
b= readline()
print("a=" + b)
print("b=" + a)

I ain't even mad

Can y'all share some good programming questions for interviewing fresh graduates?

Comments
  • 25
    Destined for management.
  • 5
    Print hello world without semicolons in C, maybe in 3 different ways 😂✌️
  • 10
    Optimization questions.
    Data structure questions.
    And difficult questions with no great solution.

    http://thecodelesscode.com/case/83
  • 11
    @Haxk20
    int add(int a, int b) {
    return a - (-b);
    }
  • 10
    On a side note, this is a terrible interview question.
  • 29
    All Hail Python:
    b, a = a, b
  • 1
    That wouldnt have run in Crystal and potentially not in Ruby :)
  • 2
    @retnikt Or JS:
    [b, a] = [a, b]
  • 1
    @htlr just curious, is it even possible? (Maybe some preprocessor or some character escaping, maybe?)
  • 1
    @htlr oh, found it. Wouldn't have thought of it, tbh...
  • 1
    @Root, +1 for the interesting resource!
  • 1
    @Root so what path would you choose on the java question? Is there a best practice approach?
  • 2
    @Wack I would move Archer and Horseman abilities to shared modules and include that functionality into the respective Archer/Horseman and Flying Rain subclasses, likely with wrappers to future-proof against divergence.

    The choice I made here is to whitelist which classes the Flying Rain can mimic, rather than blacklist from the Soldier superclass. The disjoining of abilities adds some complication as well, and future debt as the complexity grows and the classes diverge. Overall it's a little dirty and makes development more complicated, but the cleanest and easiest to learn/maintain approach I can think of.

    Thoughts?

    Note: I don't know how this (very Ruby) approach translates to Java.
  • 1
    @Wack there are many ways to architect that.

    I think the point of the article is rather to say that since requirements come from the real world and the real world is already fucked up, it's not possible to design a program modelled on the real world, in a perfect, elegant way.

    The point is also that it is the effort that counts, which applies for an interview.

    Good story @Root
  • 1
    @Root where my thoughts too.
  • 0
    Perfect solution:
    a = b
    b = a
  • 0
    @BitFlipped wouldn't work, they'll all be b
  • 4
    @ranggawiratno I feel a certain need to point out that it was a joke
  • 1
    Btw if you don't want any bad surprises, avoid adding: xor prevents overflow.
    int a = something;
    int b = something_else;
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
  • 0
    It's a neat little hack, but that's the problem: it's hacky and shouldn't be used in reality, but on the other hand it's to easy to test problem solving skills.
  • 1
    Btw, this is a terrible question. It only check if the candidate has read this riddle somewhere.
    I once was asked to detect a cycle in a linked list using only constant size memory. This is easy once you know the solution, but you need to remember that humankind needed like 10 years to invent Floyd’s algorithm.
  • 1
    Basic, but not trivial programming exercise for graduates is for a given interface, implement a decorator doing something simple on it. You don’t frame it in the language of the design patterns („decorator”), but rather describe it in your own words. For example, for an interface taking an int and returning an int, your wrapper should return the absolute value of the original result.
    You will be surprised, how many people just don’t get the concept of decorator (object oriented programming) or higher order function(functional programming). But this is what programmers do all the time in their jobs.
Add Comment