Home › Interview Questions › Given a list of employee objects with fields id, d…

Given a list of employee objects with fields id, department, and salary, how would you filter by department and minimum salary, and then sort the results?

🟡 Medium Coding Junior level
1Times asked
Sep 2026Last seen
Sep 2026First seen

💡 Model Answer

You can solve this problem with a list comprehension for filtering and the built‑in sorted function for ordering. First, filter the list: filtered = [e for e in employees if e['department'] == dept and e['salary'] >= min_salary]. Then sort: sorted_emps = sorted(filtered, key=lambda e: e['salary'], reverse=True) if you want descending salary. The overall time complexity is O(n log n) due to the sort; filtering is O(n). If you need to sort by multiple keys, use a tuple in the key function. This approach is concise, readable, and works for any number of employees. Example: employees = [{'id':1,'department':'HR','salary':50000}, ...]. The code runs in linear time for filtering and n log n for sorting, which is acceptable for typical in‑memory lists.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500