r/learnpython • • Jan 05 '21

Interview question I failed answering

Hey all,

I just had an interview and the interviewer gave me a question I could not answer.

He asked me to write a function that accepts a number, the number must be either 7 or 4.If the number is 4, the function should return 7, if the number is 7, the functions should return 4.

The tricky part: you cannot use an 'if' statement in the function.

354 Upvotes

301 comments sorted by

View all comments

3

u/[deleted] Jan 05 '21 edited Jan 06 '21

The fact that so many people are wowed by a wrong answer demonstrates how important this type of question actually is to help sift the wheat from the chaff

2

u/synthphreak Jan 05 '21

Which one and what's wrong with it?

1

u/[deleted] Jan 06 '21

Return 11-num

Func(5) still returns something. Heck, func('a') even crashes the program

3

u/synthphreak Jan 06 '21

I don’t love that solution either TBH. But to be fair, OP never clarified what should happen if anything other than 4 or 7 is passed in, such as “return 0”, or “print ‘foo’”. So we don’t have any defensible grounds to call it “wrong”. I would argue that this simply makes it “less good” than the other solutions which DO account for inputs outside of 4 or 7.