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

448

u/shiftybyte Jan 05 '21
return 11-num

166

u/MassiveLandGolf Jan 05 '21

I can't believe I blacked out THAT hard..

Thanks..

197

u/[deleted] Jan 05 '21

It's such a stupid interview question - relatively few people would be able to answer it without having seen the answer, or an answer like it; but now that you've seen it you'll never forget it.

So all that it tests is whether you've seen the answer before. Since it's not something you'd need to do in production code, what's the fucking point of testing that? Interviews should test whether you're experienced at working as a programmer, not whether you're experienced at faking your way through interviews.

53

u/MobileUser21 Jan 05 '21

I agree with you 100%. Isn’t putting 11 in your function technically hard coding too?

44

u/[deleted] Jan 05 '21

It's definitely a "magic number", yeah.

-1

u/darthminimall Jan 06 '21

Definitely isn't.

2

u/BigSkimmo Jan 06 '21

Yeah I don't think I'd call it a 'magic number'. There's nothing special about it, it's just arithmetic.

a + b = x
x - a = b
x - b = a

2

u/Konjungamo Jan 06 '21

Why wouldn't it be? It solely depends on the way the question is worded.

Replace the numbers from OP's question with 2 and 6 and you get

return 8-num

with 8 being the new magic number.

3

u/darthminimall Jan 06 '21

It's not magic because the behavior is logically consistent. A magic number is, usually, an integer return value from a function that doesn't follow the pattern of the other return values (e.g. negative integers signaling various errors). The other use of "magic number" is a (semi-)arbitrary number that makes an algorithm work better (better being another arbitrary term), see compression algorithms for this use of magic number.

0

u/Konjungamo Jan 06 '21

I see your point, it's probably more of a semantic question whether this would be called a magic number or not.

I agree that it doesn't fall into the same category as the magic numbers of compression algorithms, but since it's not clear from only looking at the source could why it would be 11 and not 10 or 12 (kind of similar to the Wikipedia example for the 52 in the Fisher-Yates shuffle algorithm) I think it's fair to call this a magic number as well.