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?

  • 21
    Destined for management.
  • 5
    Print hello world without semicolons in C, maybe in 3 different ways 😂✌️
  • 4
    #include <stdio.h>

    Int main() {
    Int a, b;
    scanf("%d", &a);
    scanf("%d", &b);
    printf("Enter the sum of the two numbers");
    scanf(%d", &a);
    printf("Im damn good program. Summing 2 numbers without using + symbol in code")
  • 2
    See ? Perfect code.
  • 9
    Optimization questions.
    Data structure questions.
    And difficult questions with no great solution.

  • 9
    int add(int a, int b) {
    return a - (-b);
  • 11
    On a side note, this is a terrible interview question.
  • 30
    All Hail Python:
    b, a = a, b
  • 1
    I like the task:
    Given an input number n,
    output a pascals triangle of n layers.
    Make sure padding is correct and dynamic.
  • 0
    That wouldnt have run in Crystal and potentially not in Ruby :)
  • 1
    @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!
  • 0
    @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.


    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
  • 0
    @Root where my thoughts too.
  • 1

    It's a fun challenge but hardly a valid interview question for determining how good a person is at programming.
  • 0
    Perfect solution:
    a = b
    b = a
  • 0
    @BitFlipped wouldn't work, they'll all be b
  • 3
    @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;
Your Job Suck?
Get a Better Job
Add Comment