Your question is Explain Code Execution and Bottlenecks. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Motorola Solutions builds dispatch software for public safety radio networks. Here is a service class that pulls together data for a precinct's dispatch summary screen.
import sqlite3
class DispatchRecordService:
def __init__(self, db_path):
self.db_path = db_path
def get_active_units(self, precinct_id):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT * FROM units WHERE precinct_id = ?", (precinct_id,))
units = cursor.fetchall()
conn.close()
return units
def get_recent_incidents(self, precinct_id):
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT * FROM incidents WHERE precinct_id = ?", (precinct_id,))
incidents = cursor.fetchall()
conn.close()
return incidents
def build_dispatch_summary(self, precinct_id):
units = self.get_active_units(precinct_id)
summary = []
for unit in units:
unit_id = unit[0]
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("SELECT * FROM assignments WHERE unit_id = ?", (unit_id,))
assignments = cursor.fetchall()
conn.close()
summary.append({"unit": unit, "assignments": assignments})
return summary
Explain what happens, step by step, when build_dispatch_summary is called for a busy precinct, and walk through the possible bottlenecks in this code. Then explain what you would change and why.