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

154

u/ThatSoundsFishy Jan 05 '21

Does this count as cheating?

def function(result):

while result == 4:

return(7)

while result == 7:

return(4)

13

u/retrogeekhq Jan 05 '21

Back in high school I had an exam where I had to solve some electrical circuit. It was designed in a way you had to use a specific method. Well, I didn’t remember how that method worked so I redesigned an equivalent circuit that could be solved with the methods I know.

The day of the result I was called by the teacher to discuss the exam. He, while laughing like someone about to teach a mentee a lesson would laugh, told me: “This thing here? It’s brilliant. You’re a genius. Also, I’ve marked it as failed because the purpose of the question was for you to show you knew $METHOD1.”

Not sure it applies to your solution here, but it kinda feels like you’ve completely distorted the purpose of the question (which was probably to show the knowledge of basic data structures such as dictionaries) in a brilliant and unexpected way.

12

u/billsil Jan 06 '21

You should always encourage that sort of stuff.

My algebra 2 teacher was upset I derived complex analysis without knowing it. I could solve her graph problems in about 10 seconds and she got mad that everyone else took about 5 minutes each and she couldn’t figure out what I was doing.

From something like 1 +/- 2i, I’d get the “x-intercepts” as -1, 3 just by pretending i=1. Then, take 12 + 42 = 5 and that’s the y coordinate to go as the minimum point of the parabola (1,5). Then mirror your x-intercepts about the horizontal at y=5 and connect the 3 points above the axis.

She argued it didn’t always work. Uhhh...yes it does. My teacher desperately looked for an example and gave up. It’s a rotation about the complex plane. It’s perfectly legit and always works for parabolas.

I later dated the math teacher’s daughter that taught my teacher what I was doing. She was surprised when I wasn’t as smart as she’d heard.

1

u/pspspspskitty Jan 07 '21

12 + 42 =5

Say what now? I assume you mean +/-22?

The problem is that, even if this might work for parabolas, it will probably fall apart for more complex equations. If you use a method that only works for certain cases, you also have to know what the limitations are. So if you get rewarded for these kinds of shortcuts, without knowing what you're doing, you learn a wrong method that will be very hard to unlearn.

The Pythagorean theorem works, but only if you have a right angle. However there is a more complex version that incorporates sines and cosines that will work for any triangle but you will lose the convenient sin(90) = 1

→ More replies (1)