Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Filter and Sort Container States
00:00
5 left

Filter and Sort Container States

MediumPython

Problem

Docker Desktop needs to display a filtered list of container states. Given container objects, keep containers whose status is allowed and whose cpu_percent meets a minimum threshold, then sort the remaining containers by multiple criteria.

Formal Specification

Implement filter_and_sort_containers(containers, allowed_statuses, min_cpu_percent, sort_criteria). containers is a list of dictionaries containing id, status, cpu_percent, memory_mb, and created_at. allowed_statuses is a list of status strings. min_cpu_percent is a number. sort_criteria is a list of dictionaries ordered from highest to lowest priority, where each dictionary has a valid field and an order of either "asc" or "desc".

Return a new list containing containers whose status appears in allowed_statuses and whose CPU usage is at least min_cpu_percent. Apply every sort criterion, preserving the original order when all specified values tie. created_at values use ISO 8601 strings, so normal string comparison is valid.

Constraints

  • 1 <= len(containers) <= 10^5
  • 0 <= cpu_percent <= 100
  • memory_mb >= 0
  • 1 <= len(sort_criteria) <= 3
  • All sort fields contain mutually comparable values.
  • Sorting must be stable for complete ties.

Function Signature

def filter_and_sort_containers(containers, allowed_statuses, min_cpu_percent, sort_criteria):
Interviewer

Your question is Filter and Sort Container States. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.