Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
0/1 Knapsack and Merge Sort
00:00
5 left

0/1 Knapsack and Merge Sort

MediumPython

Problem

Joveo needs to select campaign opportunities within a fixed budget and sort candidate scores for downstream ranking. Implement both tasks in one function: solve the 0/1 knapsack problem and sort an integer array using merge sort.

Each opportunity can be selected at most once. The knapsack result is the maximum total value whose total weight does not exceed the capacity. The sorting result must be in nondecreasing order and must not use Python's built-in sorting functions.

Formal Specification

Implement solve_knapsack_and_merge_sort(weights, values, capacity, nums).

  • weights and values are lists of integers with equal length.
  • weights[i] is the cost of opportunity i, and values[i] is its value.
  • capacity is the maximum total cost.
  • nums is a list of integers to sort.
  • Return a dictionary with keys max_value and sorted_nums.
  • max_value must be an integer, and sorted_nums must be a new sorted list.

Constraints

  • 1 <= len(weights) = len(values) <= 500
  • 0 <= capacity <= 10^4
  • 1 <= weights[i] <= capacity when capacity > 0
  • 0 <= values[i] <= 10^6
  • 0 <= len(nums) <= 10^5
  • Do not call sorted() or .sort()

Function Signature

def solve_knapsack_and_merge_sort(weights, values, capacity, nums):
Interviewer

Your question is 0/1 Knapsack and Merge Sort. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.