KatInterview

Rank Companion Items by Shared Collections

Arrays & HashingAuto-graded · 19 test cases
EasyAuto-gradedArrays & Hashinghash-mapcountingsorting

Last updated

Count how often each item shares a collection with a target item, then rank by count descending and name ascending, with an optional cap on the result length.

You are given a mapping from collection names to the items they contain, plus one target item. Find every other item that appears in at least one collection together with the target, and rank those items by how often they co-occur with it.

Task

Implement a function that takes:

  • collections: a dictionary whose keys are collection names (strings) and whose values are lists of item names (strings).
  • target_item: a string, the item to compare against.
  • limit: an optional integer, None by default.

Scoring

Process each collection independently:

  • A collection is relevant if its list contains target_item. Collections that do not contain it contribute nothing.
  • For every relevant collection, go through its list and add 1 to the score of each entry that is not equal to target_item. Scores are accumulated per list entry. Inputs normally list each item at most once per collection, in which case an item gains at most 1 per collection.
  • An item's final score is the total accumulated over all relevant collections.

Items whose score is 0 (never seen in a relevant collection) are not in the output. target_item itself is never in the output.

Ordering

Sort the [item, score] pairs by:

  1. score, highest first;
  2. item name, ascending, for equal scores. Names are compared as plain Python strings (code point order, so uppercase letters sort before lowercase letters, and "p10" sorts before "p2").

Applying limit

  • If limit is None, return the full sorted list.
  • Otherwise, return the first limit pairs of the sorted list, exactly as the Python slice sorted_result[:limit] would.
    • limit = 0 returns an empty list. It does not mean "no limit".
    • A limit larger than the number of qualifying items returns all of them.
    • A negative limit follows Python slice semantics, so -1 drops the last pair.

Output

Return a list of two-element lists [item, score], where item is a string and score is an integer. Return [] if no qualifying items exist, for example when the target appears in no collection, appears only in collections alone, or collections is empty.

Constraints

  • Collection names and item names are strings.
  • limit is None or an integer.
  • The input fits in memory; a single pass over all collections is sufficient.

Examples

Example 1

Input: rank_companion_items({'b1': ['a', 'c', 'd', 'e'], 'b2': ['a', 'c', 'e', 'f'], 'b3': ['a', 'c', 'g', 'i']}, 'a')

Output: [['c', 3], ['e', 2], ['d', 1], ['f', 1], ['g', 1], ['i', 1]]

Example 2

Input: rank_companion_items({'b1': ['a', 'c', 'd', 'e'], 'b2': ['a', 'c', 'e', 'f'], 'b3': ['a', 'c', 'g', 'i']}, 'a', 3)

Output: [['c', 3], ['e', 2], ['d', 1]]

Example 3

Input: rank_companion_items({'board_1': ['p1', 'p2'], 'board_2': ['p2', 'p3'], 'board_3': ['p1', 'p3', 'p4'], 'board_4': ['p1', 'p2', 'p4']}, 'p1', None)

Output: [['p2', 2], ['p4', 2], ['p3', 1]]

Hints

Hint 1

Only collections that contain the target item can contribute to any score, so skip all the others.

Hint 2

Keep a dictionary from item to running count. For each relevant collection, add 1 for every entry except the target itself.

Hint 3

Convert the dictionary into [item, score] pairs, sort with a key of (negative score, item name), then slice with result[:limit] only if limit is not None (limit 0 gives an empty list).