Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

You need to log in / sign up to run or submit.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.