1

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?

2
  • 4
    Tell the interviewee what data types you expect the function to accept and return. Or, just provide the Java version to the interviewee (most decent candidates should be able to figure out what it means from the context). Jan 7, 2014 at 22:34
  • Put the expected types in a comment? Jun 19, 2015 at 14:01

5 Answers 5

4

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

2

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)]
1

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.

0

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

0

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.

2
  • 1
    I'd be nervous about calling the thing you're asking for a "method" there. If I was asked that in an interview, I'd first want the interviewer to clarify that they mean "method" (function attached to object) rather than just "function". Since you seem to be asking for a method without specifying a parent object.
    – Racheet
    Jan 8, 2014 at 11:25
  • 1
    Well it would seem the goal is to sort, now you can figure out what class such a method would belong to (or just choose one, or in fact tell the interviewer that you can find several solutions). Jan 8, 2014 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.