Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-307

Top-K Exact vs Approximate Check

Problem

Check whether an approximate top-k result matches the exact one.

items is the full observation list, approximate is a claimed top-k collection, and k is the requested size. The exact result orders by count descending and then value ascending.

Return True when the two sets match, otherwise False.

Hint

Both lists are sorted before comparison so order within the approximation does not matter.

Test cases

case 1
in: [["a","b","a","c","a","b"],["a","b"],2]
expected: true
case 2
in: [["a","b","a","c","a","b"],["a","c"],2]
expected: false
case 3
in: [[],[],1]
expected: true
case 4
in: [["x"],["y"],1]
expected: false
Starter codePython
def topk_check(items, approximate, k):
    # Your code here
    pass

4 test cases · run your code against them in the editor