We're looking to hire a new position that is a bit more senior than the previous programmers we've hired. Was thinking of asking this question in the exam / screening question. My intent is to provide a plausible (though stripped down) example of what I've seen and to see if the person can think through how to provide feedback but without having to provide a ton of domain knowledge. Not sure how much time we are giving for this exam right now (previously we just sent it to them electronically and provided a day to respond).
> Another programmer was tasked with writing a function that takes a list of teacher locations and a list of student locations. For each student it should return the ten (10) closest teachers. Locations are represented by latitude and longitude. They submit the following code for you to review. What comments, if any, would you have on this?
from math import sqrt
class Location(object):
def __init__(self, id, lat, lng):
self.id = id
self.lat = lat
self.lng = lng
def distance(self, other):
return sqrt(
pow((self.lat - other.lat) * 69.1691, 2) +
pow((self.lng - other.lng) * 54.6, 2)
)
def find_closest_teachers_for_students(teachers, students):
for student in students:
dists = [
(student.distance(t), t.id)
for t in teachers
]
dists.sort()
yield student.id, [tid for _, tid in dists[:10]]
if __name__ == '__main__':
teachers = [
Location(0, 36.4764814028762, -126.99754149850868),
Location(1, 39.74147516717284, -126.17793399657747),
Location(2, 41.643785297568094, -120.9365835402883),
Location(3, 39.1802873704446, -117.4268783685548),
Location(4, 33.16174320316393, -115.4948990935354),
Location(5, 33.74166203009007, -120.63872559860664),
Location(6, 43.09220658641743, -125.77352618891415),
Location(7, 40.07790486408948, -122.51123541242445),
Location(8, 42.77574441624258, -123.2849185828985),
Location(9, 38.013030782991905, -115.31284422245481),
Location(10, 34.4140224619137, -125.9723692100629),
Location(11, 35.13315035147819, -117.69784858760552),
Location(12, 43.83216220706978, -116.73811809675662),
Location(13, 38.873833691592246, -116.36769320889711),
Location(14, 42.518523038160794, -123.65914767025608),
Location(15, 43.09028850838418, -122.04327075725462),
Location(16, 34.60595739647577, -121.10855832551158),
Location(17, 34.4963055286892, -120.14022089482256),
Location(18, 34.84967944275003, -126.18209876475903),
Location(19, 38.774392592661904, -119.898342994481),
]
students = [
Location(0, 39.762208470564225, -119.40605947102112),
Location(1, 32.958968394618005, -121.7327970911715),
Location(2, 39.04992736552751, -121.8824078357889),
Location(3, 41.863139668619255, -121.78830188780077),
Location(4, 37.57448941499755, -118.4871593680229),
Location(5, 38.86736759106722, -115.31454505655249),
]
expected_results = {
0: [19, 3, 2, 7, 13, 9, 15, 8, 14, 12],
1: [5, 16, 17, 10, 11, 18, 4, 0, 19, 3],
2: [7, 19, 2, 1, 3, 14, 8, 15, 13, 16],
3: [2, 15, 8, 14, 7, 6, 19, 1, 3, 12],
4: [19, 3, 13, 11, 9, 17, 16, 7, 5, 2],
5: [13, 9, 3, 19, 11, 12, 2, 4, 17, 7],
}
for student_id, close_teachers in find_closest_teachers_for_students(
teachers, students):
assert expected_results[student_id] == close_teachers