KatInterview

Two-Part Challenge: Token Frequencies and Multiset Overlap with a Barebones RDD

Data Structure DesignModel answer
EasyModel answerData Structure Designrddword-countmultiset-intersection

Last updated

Using only flatMap, groupByKey, union, and collect on a minimal RDD class, implement word counting and a duplicate-preserving intersection of two datasets.

You have access to a stripped-down RDD-like class that deliberately omits most of the usual operations. The only methods available are flatMap, groupByKey, union, and collect.

from collections import defaultdict

class RDDLite:
    def __init__(self, data):
        self.data = data

    def flatMap(self, fn):
        """Apply fn to every element, concatenate all returned iterables, return new RDDLite."""
        out = []
        for x in self.data:
            out.extend(fn(x))
        return RDDLite(out)

    def groupByKey(self):
        """Assume elements are (key, value) pairs. Return RDDLite of (key, [values])."""
        buckets = defaultdict(list)
        for k, v in self.data:
            buckets[k].append(v)
        return RDDLite(list(buckets.items()))

    def union(self, other):
        """Return a new RDDLite containing all elements from both RDDs."""
        return RDDLite(self.data + other.data)

    def collect(self):
        """Return a plain Python list of all elements."""
        return list(self.data)

There is no map, filter, reduceByKey, or distinct. Your solutions should work by composing the four available operations rather than by calling collect() up front and processing the raw list in Python.


Part 1 — Token frequency table

Write word_count(rdd) that accepts an RDD of text strings and returns an RDD of (word, count) pairs. Tokenize on whitespace only; treat case as-is.

lines = RDDLite(["the cat sat", "the dog ran", "the cat ran"])
word_count(lines).collect()
# [('the', 3), ('cat', 2), ('sat', 1), ('dog', 1), ('ran', 2)]

Points to address:

  • How you emulate map with flatMap
  • How you turn a grouped value list into a numeric count
  • The time and memory cost of the approach, especially compared to reduceByKey

Part 2 — Multiset intersection

Write intersect(rdd_a, rdd_b) that returns an RDD of every element shared by both inputs. If a value appears c_a times in the first RDD and c_b times in the second, it should appear min(c_a, c_b) times in the output.

a = RDDLite([2, 2, 2, 5])
b = RDDLite([2, 2, 7])
intersect(a, b).collect()
# [2, 2]

Points to address:

  • Stating the multiplicity rule before writing any code
  • How to track which source each element came from using the available operations
  • What happens to elements that exist on only one side
  • Memory cost when a single value is extremely frequent

Hints

Hint 1

flatMap can stand in for map by always returning a one-element list, and for filter by returning an empty list — you don't need any missing method.

Hint 2

After groupByKey gives you (word, [1, 1, 1, ...]), the count is just len() of the values list, and another flatMap can replace that list with a single (word, count) pair.

Hint 3

Before calling union, tag every element in rdd_a as (element, 'a') and every element in rdd_b as (element, 'b') — then groupByKey separates the two sides naturally and you can compute min(count_a, count_b) per element.