Divide Campaign Line Items Into K Groups With Identical Spend

Last updated: November 29, 2026

Quick Overview

Given positive spend amounts for ad line items and an integer k, decide whether the items can be divided into k groups whose total spend is the same.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given positive spend amounts for ad line items and an integer k, decide whether the items can be divided into k groups whose total spend is the same.

Meta ads finance occasionally needs to split invoices evenly across billing entities, which frames this NP-hard partition problem. The interviewer expects backtracking with sorting, duplicate skipping and a group-by-group fill strategy, plus the bitmask DP alternative.

What the Interviewer Expects
  • Check divisibility and the largest item first
  • Fill one group at a time toward the target
  • Sort descending so failures surface early
  • Skip equal values after a failed try
  • Stop if the first item of an empty group cannot be placed
  • Mention the O(n * 2^n) bitmask DP
Key Topics to Cover
Backtracking
Bitmask DP
Partition problem
Pruning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the actual groups?
  • How would you solve it for n = 20 with memoization over used masks?
  • What if amounts can be negative?
  • What if groups must also have equal item counts?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Up to 16 items, positive integers, k from 1 to 16.
  • Every item goes into exactly one group; groups may not be empty when amounts are positive.
  • Return a boolean....
High-Level Design

Let target = total / k; if it is not an integer or the largest item exceeds it, return False. Sort descending and fill groups one at a time: scan forward from start adding unused items that fit, a...


Submit Your Answer
Markdown supported