🚀 AI-Powered Mock Interviews Launching Soon - Join the Waitlist for Early Access

technicalmedium

Given a list of properties, each with a unique ID, a list of tenants currently occupying properties (tenant ID, property ID), and a list of maintenance requests (request ID, property ID, status), write a Python function that returns a dictionary mapping each property ID to the number of active maintenance requests and the number of current tenants.

technical screen · 10-15 minutes

How to structure your answer

The ideal answer employs a MECE (Mutually Exclusive, Collectively Exhaustive) approach to data aggregation. First, initialize a dictionary to store results for each property, setting initial counts for active maintenance requests and tenants to zero. Second, iterate through the tenants list, incrementing the tenant count for each property ID in the results dictionary. Third, iterate through the maintenance requests list, checking if the request status is 'active' (or similar criteria) and incrementing the active maintenance request count for the corresponding property ID. Finally, return the populated dictionary. This ensures all properties are covered and counts are distinct.

Sample answer

def analyze_property_data(properties, tenants, maintenance_requests):
    property_summary = {prop_id: {'active_maintenance_requests': 0, 'current_tenants': 0} for prop_id in properties}

    for tenant in tenants:
        property_id = tenant['property_id']
        if property_id in property_summary:
            property_summary[property_id]['current_tenants'] += 1

    for request in maintenance_requests:
        property_id = request['property_id']
        if property_id in property_summary and request['status'] == 'active': # Assuming 'active' is the status for active requests
            property_summary[property_id]['active_maintenance_requests'] += 1
            
    return property_summary

# Example Usage:
properties_list = ['P101', 'P102', 'P103']
tenants_list = [
    {'tenant_id': 'T001', 'property_id': 'P101'},
    {'tenant_id': 'T002', 'property_id': 'P101'},
    {'tenant_id': 'T003', 'property_id': 'P102'}
]
maintenance_requests_list = [
    {'request_id': 'M001', 'property_id': 'P101', 'status': 'active'},
    {'request_id': 'M002', 'property_id': 'P101', 'status': 'completed'},
    {'request_id': 'M003', 'property_id': 'P102', 'status': 'active'},
    {'request_id': 'M004', 'property_id': 'P103', 'status': 'pending'}
]

result = analyze_property_data(properties_list, tenants_list, maintenance_requests_list)
# print(result)
# Expected Output: {'P101': {'active_maintenance_requests': 1, 'current_tenants': 2}, 'P102': {'active_maintenance_requests': 1, 'current_tenants': 1}, 'P103': {'active_maintenance_requests': 0, 'current_tenants': 0}}

Key points to mention

  • • Efficient data structure initialization (e.g., using a defaultdict or pre-populating the result dictionary with all property IDs).
  • • Clear definition of 'active' maintenance request status (e.g., 'open', 'pending', 'in_progress' vs. 'closed', 'resolved').
  • • Handling edge cases: properties with no tenants or no maintenance requests.
  • • Time complexity analysis: O(P + M + T) where P is the number of properties, M is maintenance requests, and T is tenants.
  • • Space complexity analysis: O(P) for the result dictionary.

Common mistakes to avoid

  • ✗ Not initializing counts for all properties, leading to KeyErrors if a property has no tenants or requests.
  • ✗ Incorrectly defining 'active' status for maintenance requests, leading to inaccurate counts.
  • ✗ Inefficient nested loops instead of direct lookups or single passes.
  • ✗ Modifying the input lists during iteration, which can lead to unexpected behavior.
  • ✗ Forgetting to handle properties that might exist but have no associated tenants or requests.