Question

If I'm giving an interview coding question in Java, I can specify the most of the question just by giving a method signature. (Made-up example follows.)

public class Table {
  public String identifier;
  public int seatCount;
}

public static List<String> tablesWithEnoughSeats(List<Table> tables, int minSeats)

If the candidate prefers Python, how do I present the problem for them? The Python method signature doesn't specify the data type. Is there some standard Python way of doing this?

If I look at Python coding challenges online, they tend to specify the requirements as taking certain input to produce certain output. I don't want the candidate to waste their time writing code to parse an input file. (My example has just String and int, but the actual interview problem might contain more complex data.) What's the best way to express the parameter constraints so that the candidate can implement the algorithm I'm interested in without doing a bunch of plumbing?

Was it helpful?

Solution

Mock your inputs. Say "Assume that this array consists of integers or floats or whatever". You can also annotate things with comments.

I'd write this in Python like so:

class Table: #identifier: string, seat_count: int
    def __init__(self, identifier, seat_count):
        self.identifier = identifier
        self.seat_count = seat_count

I'm prone to writing Python functionally so I'd instantiate a list of tables then call a function that checked each table to see if had enough seats or not. I'd probably use a filter to that. Could also do a list comprehension. The latter is more "Pythonic".

OTHER TIPS

Give them the data structure (explain namedtuple if they haven't seen it before):

from collections import namedtuple
Table = namedtuple('Table', 'name seats')

Then show them as sample invocation:

>>> tables = [
...     Table(name="First", seats=6),
...     Table(name="Second", seats=4)
... ]
>>> tablesWithEnoughSeats(tables, 5)
[Table(name="First", seats=6)]

Or just use annotations in function, and comments in class?

class Table:
    def __init__(self, identifier, seat_count):
        self.identifier = identifier  # str
        self.seat_count = seat_count  # int

def tablesWithEnoughSeats(tables: [ Table ], minSeats: int) -> [str]:
    pass

WARNING: works only in p3k, p2.x doesn't support annotations. I'd use comments instead:

def tablesWithEnoughSeats(tables,  # list of Table
                          minSeats # int
                          ):       # return list of str
    pass

Other option is docstring with some markup language, but I don't really like this idea.

If the actual syntax doesn't matter to you (which sounds like the case), explain the method signature and allow them to write pseudo-code to fill in the algorithm. Then, it is effectively irrelevant which 'language' they choose to use as long as the interviewee can explain their thinking to you.

There's no harm in guiding them somewhat to; for example explicitly stating that you don't need them to parse an input file. If they go down that route say something like "imagine we already have a function to parse the file and it returns an array of strings, what is your next step?"

I would expect any sufficiently trained and experienced programmer wouldn't even need the method signiature for a mock question (in practise is an entirely different matter).

  • I am managing a restaurant as a simple list of table objects
  • Each table has a name and a fixed number of seats.
  • Given a list of tables and a required number of seats for a given party, write a methods that finds and returns a second list of all tables that could accomodate that party.
  • As an optional extra, sort the list so as to minimise the number of "wasted seats" at a table.

Now, the names of properties and the class definition can all be abstracted away and they can get to the business of writing the actual code.

Licensed under: CC-BY-SA with attribution
scroll top