How to Create a Python Method Prototype

prototypingpython

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?

Best Answer

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".

Related Topic