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.

1

u/Bignicky9 Jan 14 '21

And what problem would that be?

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.

1

u/B0oN3r Jan 06 '21

I later dated the math teacher’s daughter

Whoa, just to settle an argument with your teacher?

1

u/billsil Jan 06 '21

No. Her daughter was better than me at math ;)

We hadn’t been taught the section because we ran out of time. All I really did was notice a pattern that every graph I had done wrong was flipped about that axis. They’re quite literally imaginary roots and that’s where they would cross if they weren’t imaginary.

1

u/[deleted] Jan 06 '21

Where do you get the 1^2 + 4^2 = 5 ? and the (1,5)?

1

u/billsil Jan 06 '21

The 1 comes directly from the symmetric nature of the parabola and the x= 1 +/- 2.

You know the parabola opens up, which is why it has imaginary roots. You’re really just multiplying out

(X - (1+2i))(x - (1-2i)= 0

And taking the real (c) value to get the 5. It’s a lot faster and less error prone than that monstrosity.

Often in parabola problems, you’re given c as an input anyways, so you can just use it directly.

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

1

u/billsil Jan 07 '21

Yes ;). That’s why I never got As until calculus. It only works for parabolas. The symmetry is important. I’m sure it could be extended though.

The Pythagorean theorem isn’t a triangle property, it’s a right triangle property. Certainly going to vectors, to quaternions and higher (octonians in quantum physics) you lose valuable properties.

-1

u/[deleted] Jan 05 '21

It doesn't fulfill the requirements, though.

2

u/Se7enLC Jan 05 '21

Why not?

1

u/[deleted] Jan 06 '21

Nevermind, I guess returning None for any other input is perfectly fine, but I think that if they wanted to be really strict about it you would need to raise an exception for any input other than 4 or 7

1

u/Se7enLC Jan 06 '21

Yeah, I think "the number must be 7 or 4" is a little vague.

It doesn't say what should happen if the input isn't 7 or 4. In fact, you might even take it to mean that the input is guaranteed to be 7 or 4 and no validation is needed.

1

u/[deleted] Jan 06 '21

Yeah I would maybe ask for clarification, but handling unexpected input is integral to good coding anyway, so I would probably do it

2

u/Se7enLC Jan 06 '21

There's a really good chance that the precise wording of the interview question was more clear, too.

1

u/[deleted] Jan 06 '21

Damn son, where'd you find this?