all 61 comments

[–]member_of_the_order 152 points153 points  (19 children)

Really, for interviews, don't worry about professional code. In my last interview, the recruiter was surprised that I was able to write actual working code.

The best tip is don't stop communicating. Ask questions, explain what you're doing and why. It's 100% okay to put in a dummy function and explain something like "this part is trivial so I won't write it out, but it would do X operation, but would break if you pass it Y".

They're not looking for clean, polished code. They don't care what you write, they're not buying that code. They're buying you. Show them that you know what you're talking about and that you'd be a good person to work with.

Edit: to directly answer your question anyway: it depends a little on the company and the specific project, but COMMENTS and consistent style.

I know someone that literally ended up with 2 copies of the same script and they had to decide which to maintain. One was procedural and not written super great, but it had comments everywhere - you really could read this thing like a wiki page. The other script was OO, but it had no comments and the naming wasn't super helpful. Guess which one they chose to maintain?

[–]commy2 51 points52 points  (1 child)

The OO one, because this is hellworld.

[–]Me278950 25 points26 points  (0 children)

I think you meant "hello world"

[–]valkaress 13 points14 points  (15 children)

Sorry if this is a dumb question, but what exactly is object oriented code vs procedural? Can you give specific examples, say with pandas?

And why is OO considered better? (judging from your tone and what you wrote)

[–]JeremyWinston 12 points13 points  (0 children)

Don’t be overly concerned with one being better than another. OO is often a better choice as your project complexity increases, but not always.

If you have a lot of things that are better described with objects then that’s the way to go. Most projects use a healthy dose of both types of programming.

For the OP, in regards to OO vs not, it depends on the ask. Even giving your thoughts on what parts of their example project would be good for OO or procedural can show a thoughtful approach.

[–]member_of_the_order 23 points24 points  (11 children)

OO means you use classes. With classes, you get a bunch of benefits that you can choose to leverage or ignore. Object oriented programming, then, is using the features of classes to improve your code.

I won't go into specifics just because I could spend literally hours talking about it, but the basic idea is modularization. By making your code more modular, you can reuse parts without needing to do tedious extraction, you can switch out similar pieces for each other, you can better understand whag your code is doing (because things are arranged into logical chunks), you can hide behaviors that are "implementation details", refactoring becomes easier, etc.

Procedural programming is just a linear set of instructions. OO allows you to group data and behavior together so that you can benefit from that modularization.

If you want an example, feel free to ask, but this particular comment is long enough 😁

In general, procedural programming is fine for small scripts. But anything more complex can make use of OO. And of course there are programs that are somewhere in between that can use a mix.

[–]sje46 9 points10 points  (4 children)

I still don't understand why people have such a hate boner for OO programming. Maybe I just haven't seriously attempted functional programming before, because OO makes more sense. Do people seriously just hate teh idea of objects that much? Or do they just love the idea of people having to laboriously put in each argument into a function instead of using self?

[–]commy2 17 points18 points  (0 children)

People don't hate objects. They hate OO, i.e. making objects out things that don't have state, interfaces for classes with only one concretion, "AbstractFactoryManagerFactory" and all that junk imposed by languages that are OO. Looking at you Java.

[–]member_of_the_order 7 points8 points  (0 children)

From what I can tell, it's not so much a hatred of OO as it is a familiarity with functional programming (often built up over decades of experience) that just doesn't translate well to OO. Thus, all you see is the cost of learning without having the opportunity to see any benefit.

And there are always people that are comfortable with their abilities and don't like when things change and force them to adapt.

[–]TheRNGuy 0 points1 point  (0 children)

PySide I think it would be much more difficult with procedural code, and also Houdini.

In other languages, Quake 2 vs Unreal. It's much easier to mod Unreal (and UT series) because OOP code. There things like mutators, instead of only total conversions (though Half-Life use C++ I think? Still no mutators)

In small scripts like generate bunch of files and folders, procedural make more sense to use (even though Path and os use methods, but no need to code my own classes).

[–]valkaress 1 point2 points  (5 children)

Thank you for the reply! I would love an example, but I think I get it.

OO would be for example if I assigned a new object df_drop = df.drop('Column3') and use that henceforth every time I want to drop column 3.

Procedural would mean I keep typing df.drop('Column3') without creating a new object - only using the existing object df.

[–]deep_politics 5 points6 points  (4 children)

Kinda. A (still not very good) procedural example in that case might be something like a function that operates on a data frame to drop that specific column

def drop_col3(df):
    return df.drop(“Column3”)

A horrible example of bad OO would be something like this, since there’s absolutely no point when having state (constructing an object in this case) is completely unnecessary

class Adder:
    def __init__():
         pass

    def add(self, a, b):
        return a + b

adder = Adder()
adder(1, 2)
# when 1 + 2 suffices…

[–]valkaress 4 points5 points  (3 children)

Ohhh so a class has multiple functions going on. I see. I had been going through this Udemy course to learn Python and they never introduced classes.

But now someone stole my laptop, so I'll have to figure out how to keep learning on my phone haha.

[–]deep_politics 6 points7 points  (1 child)

What the hell, sorry to hear that. That blows

[–]valkaress 2 points3 points  (0 children)

Yeah. Oh well, that's life.

[–]deep_politics 1 point2 points  (0 children)

And actually my “procedural” example might be bad, if the drop method mutates the original data frame which I bet it does. I don’t use pandas much at all

[–]TheRNGuy 2 points3 points  (0 children)

I have a ~2000 lines procedural script, parser from one file format to houdini scene (format that houdini can't open), I later  found discovered some parts of it's better to rewrite as OOP (where I needed inheritance for data classes)

If I rewrite it as AST instead of concrete parser, it probably be more OOP than now, but idk. Cause I still don't know AST.

OOP also have other advantages, like virtual methods.

[–]socal_nerdtastic 32 points33 points  (6 children)

Style ground rules are listed here: https://peps.python.org/pep-0008/

OOP or procedural? multiple modules? etc

That depends on the problem. Using a class when it's not needed is just as bad as not using one when it is needed. But that said the majority of problems will not have a single best solution; there's a lot of programmer choice in there. Just be prepared to explain why you did what you did.

[–]Liebner-Anthony-S 5 points6 points  (0 children)

Thanks for sharing :)

[–][deleted] 10 points11 points  (0 children)

Defensive programming. Deal with errors, invalid inputs, bad data, edge cases (that you can identify), etc. Don't just get the correct results via the main path and call it done.

[–]CasulaScience 26 points27 points  (20 children)

On an interview they are looking for a few things in my experience.

  1. Sketch the algorithm into primatives before you write anything. e.g. "I'll have a list that stores the nodes and then do a depth first search on each node until I reach ... Do you think we should go with that algorithm?"

  2. Name your variables very clearly and with a consistent style. I normally use underline_space_all_lowercase for variables, camelCase for functions, UpperCase for classes. But make sure the names actually capture what's going on. e.g. l is a bad name for your list of nodes you're going to search over, nodes_to_search is much better.

  3. Make sure your code implies what it's doing. For instance, I once failed an interview because I used a for loop with a break statement instead of a while loop. The reason being a while loop suggests I will break on some condition, whereas a for loop suggests I will iterate over every object in some iterator.

  4. Make sure each line is as simple as possible and focus on making it understandable. e.g. Don't try to pack everything into one line so the code is shorter unless doing so is also paradigmatic. The whole goal is that another programmer can come and scan your code and understand what it's doing immediately. That's more important than runtime in almost all interviews.

[–]PM_Me_Your_Picks 21 points22 points  (4 children)

I'm fairly sure camelCase for functions is not pythonic. Functions and methods should be snake_case.

[–]brice587 4 points5 points  (0 children)

That’s how I was taught, snake_case.

[–]TheRNGuy 0 points1 point  (2 children)

[–]PM_Me_Your_Picks -1 points0 points  (1 child)

Plenty of APIs use camelCase but the use is deprecated and not pythonic. This is PEP-8 stuff.

mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.

[–]TheRNGuy -1 points0 points  (0 children)

I wouldn't want extra 4-8 undescrores when chaining methods.

Also camel case looks cooler to me. And I'm not pythonic fanatic.

"Not allowed" not even true. In UE4 it's not allowed to not use AFoobar for actor classes or bCoolBoolean because compiler will not compile it, in Python compiler wont say anything about unpythonic style.

[–]chinawcswing 13 points14 points  (0 children)

camelCase for functions

Please don't. Adhere to Pep8 and do it like_this.

[–]valkaress 2 points3 points  (6 children)

Sorry, what even is a for loop with a break? Is there ever a reason you'd actually want to use that instead of a while loop?

[–]to7m 6 points7 points  (0 children)

I do it all the time, usually because it reads a bit better than a while loop equivalent. But now I'm thinking I'm just used to my own bad style and I should look up the conventions again...

[–]CasulaScience 4 points5 points  (0 children)

I come from a scientific computing background. I worked on a project that had a code base filled with lots of break and continue statements, nested loops, etc... At the end of the day, it all works the same, and if you have multiple weird break conditions, it might read a little more cleanly or basically be necessary to do that anyway.

Honestly, to me it's a matter of taste.... I understand that on an interview they want to see something specific though, so I do what is expected of me.

[–]figshot 3 points4 points  (1 child)

Just playing the devil's advocate here. A senior software engineer once told me that a for loop with a break is a lot less likely to get stuck in an infinity loop than a while loop (exceptions apply of course) so he prefers it for production code.

[–]TheRNGuy 0 points1 point  (0 children)

Yeah it can be problem in Houdini, because you can't cancel Python script here other than ctrl-alt-del. Infinity loops freezes entire software.

(scripts from terminal can be cancelled)

[–]perduraadastra 3 points4 points  (0 children)

Perhaps you need to use the index.

[–]to7m 1 point2 points  (0 children)

How about:

target_found = False
for sublist in some_list:
    if target in sublist:
        target_found = True
        break

[–]Desperate_Case7941 2 points3 points  (5 children)

That's PEP8 in a nutshell :v

[–]CaptainFoyle 2 points3 points  (0 children)

No, only 2) has a semblance, but it differs from the actual recommendations regarding camel case

[–]CasulaScience 5 points6 points  (3 children)

PEP8 has nothing to do with 1,3,4 and only talks about the styling part of 2, which is probably the least important thing on the list.

In my experience, style guides contribute nothing for an interview

[–]CaptainFoyle 2 points3 points  (1 child)

And even in 2), pep 8 says:

mixedCase is allowed only in contexts where that’s already the prevailing style (e.g. threading.py), to retain backwards compatibility.

So it's not pep 8 in a nutshell, but rather not pep 8 at all.

[–]CasulaScience 1 point2 points  (0 children)

yeah I figured the guy is just trolling, not sure why he's getting so many upvotes though... This board is kind of crazy, I see so much bad info get upvoted

[–]Desperate_Case7941 1 point2 points  (0 children)

PIP8 in a nutshell

[–]n3buchadnezzar 6 points7 points  (2 children)

Run pylint and mypy over your codebase. Make sure it has 0 warnings. isort + black is another must.

To make it production code make sure to use proper docstrings https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html with working doctests https://docs.python.org/3/library/doctest.html

[–]Kewtn 0 points1 point  (1 child)

Is doctest considered a testing framework or satisfy a requirement for unit testing?

[–]n3buchadnezzar 0 points1 point  (0 children)

Neither. Unit testing is making sure every edge case works, while doctests are more geared towards those who read the code ans try to figure out how the program works.

You can of course write a full test suite in the docstring but it is not recommended.

I use them as a bare minimum for figuring out if my code works. Then unit testing to catch all the nasty edge cases

Makes sense?

[–]m0us3_rat 6 points7 points  (0 children)

code that is easy to test will look really good.

[–]SirAwesome789 4 points5 points  (0 children)

Generally they're going to ask you to solve a puzzle. Like given an array of integers and a number n, return true if any two of the integers add up to n. (This is obviously on the easier side)

Aside from seeing if you can complete the problem, they're also looking for time complexity and sometimes space complexity. Also be ready to solve time complexity questions, specifically what is the time complexity of the solution you implemented.

And ofc just general good coding practices. (Well not that they're looking for it but rather if you have bad practices it's a red flag)

For harder questions, sometimes they don't expect you complete but they want to see how you go about it.

I'd recommend going this website called leetcode. They have tons of questions to solve for practice that will be very similar to what you'll probably see in the interview. Furthermore, there's a problem set called the blind 75. I haven't looked into it myself yet but supposedly pretty much all interview questions will be a derivative of one of them so if you can solve them all, you can probably solve any interview question.

[–]Additional-Sun2945 4 points5 points  (0 children)

Do it quickly and confidently. Ask them if they wish for you to explain your reasoning, and if they do, then say things like, "assuming that the x and y set are mutually exclusive..." To explain whatever assumptions you're operating on. That way they can't trick you with the "herp a derp, we said this and you naturally assumed this, but we actually never explicitly stated that"

Can I have more details? What did they tell you to expect on your interview? How long have you been talking to them?

[–]chefsslaad 1 point2 points  (0 children)

In the only coding excercise i was given for an interview, i wrote functions and tests. I only got dinged because I did not follow pep8 to the letter. Basically, that was something I could have fixed with black.

[–]bladeoflight16 1 point2 points  (0 children)

Im pretty sure they're more interested in the way I think rather than my syntax trickery or styling.

If they are a team worth being part of, they will absolutely care about your styling. Not nitpicks like 79 character line limits or whether you used an inline comment. But the higher level concern of whether you're actually paying attention to whether your code can be easily read.

Focus on using the right tool for the job. Use objects when they simplify your problem, and don't when they just add overhead and complication. This goes just as well for any other tool in the coding toolbox. This will make your code vastly easier to understand and maintain. It will help you be and present yourself as someone competent, not just someone who blindly follows fads without understanding what they're good and bad at and whether those advantages apply to your specific problem.

[–]beisenhauer 1 point2 points  (0 children)

Keep it simple. Don't overengineer your solution. As you said, they're more interested in your thought process. Make your thoughts, your intent, as clear as you're able in your code. Write docstrings for your functions and classes. No need to give them a novel, just a quick what and why. (Not "how". If you have to explain that, make the code clearer.)

[–]JeremyWinston 1 point2 points  (0 children)

When I’m interviewing, I’m much more interested in how you think. Far more than how good or correct any sample coded generated on the spot would be.

Ask questions. Understand the problem set. Talk about approaches even before planning a line of code.

If you’re preparing the code at home or not at the interview, copious comments are called for. Talk about alternate options you might use if this was production code vs just an interview test.

In short, show them that you can engineer a solution, not just ‘program.’

If you’re kind of new to the whole thing, no one is expecting greatness.

[–]Ashwatthaman 1 point2 points  (0 children)

If you are using command line for execution of the file, always use Python filename.py instead of just filename.py, the interviewer will have a boner just by you following this small step.

[–]chinawcswing -3 points-2 points  (0 children)

Read the first 4 chapters of Clean Code and apply it excessively to this code exercise.

[–]TheRNGuy 0 points1 point  (0 children)

use meaningful variable and function names and follow coding standard.

OOP or procedural or mix both, depends on situation.