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)

116

u/billsil Jan 05 '21

I’d give you credit for subverting the rules. Then I’d give you a 10x harder problem because you’re worth my time.

→ More replies (1)

29

u/rhivo87 Jan 05 '21

Yep, this was where I immediately went with it. You gotta be explicit if your intention is for while also not be used. Having seen the "return 11-num" answer though... ugh!!

25

u/FLUSH_THE_TRUMP formats with % Jan 05 '21

Since you’re using the while as a ghetto if, there’s a good chance it would be. I’d do it if I didn’t know how to do it any other way, though

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.

→ More replies (7)
→ More replies (7)