Rerank Wholesale Search Results to Cap Consecutive Same-Brand Products

Last updated: November 29, 2026

Quick Overview

Given search results already sorted by relevance as (product_id, brand_id), reorder them so that no more than k products from the same brand appear consecutively, while keeping the order as close to the original ranking as possible. If the constraint cannot be satisfied, append the remaining products in score order.

Faire
Coding & Algorithms
Software Engineer
Faire
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given search results already sorted by relevance as (product_id, brand_id), reorder them so that no more than k products from the same brand appear consecutively, while keeping the order as close to the original ranking as possible. If the constraint cannot be satisfied, append the remaining products in score order.

Large brands can flood a page of wholesale search results with their own catalog, which hurts retailers looking to discover new makers and hurts smaller brands. This tests a greedy reranking pass on top of a ranking model's output and whether you can reason about when greedy is good enough.

What the Interviewer Expects
  • Use a greedy pass that always takes the highest ranked eligible item
  • Track the brand run at the tail of the output
  • Handle the case where only one brand remains
  • Explain why greedy preserves relevance order as much as the constraint allows
  • Discuss complexity and how to make it faster for long lists
Key Topics to Cover
greedy algorithms
reranking
diversity constraints
search results
list manipulation
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 change this to cap total products per brand in the first page rather than consecutive ones?
  • How would you make this O(n log n) or better for thousands of results?
  • How do you measure whether diversity hurt or helped conversion?
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

Input: a relevance-ordered list and integer k at least 1. Output: a permutation where any window of k + 1 consecutive items contains at least two brands, where possible. Among valid choices we prefer ...

High-Level Design

Build the output one slot at a time. At each slot, scan the pending list from the top and take the first item whose brand would not extend a run of k identical brands at the tail. If no pending item i...


Submit Your Answer
Markdown supported

Related Questions