Asked at XTX Markets, Squarepoint Capital
Theory: Quant Python That Survives ReviewRead the problem, hints and solution here. The editor needs a bigger screen: open this page on a laptop to write and run your code.
Almost every equity factor is used as a rank rather than a level. The raw number is not comparable across time, because a value of 30 might be the best in the universe one day and the worst another, so research code ranks each day's cross section and trades the rank.
Implement rank_cross_section(rows).
Each row is a (symbol, date, value) tuple. symbol is a string, date an
integer, and value is an integer or None when the factor could not be
computed for that symbol that day. Rows arrive in no particular order.
For each date, rank the symbols that have a value:
None are excluded entirely, not ranked last.Return a list of (date, symbol, rank), sorted by date ascending and by
symbol ascending within a date.
rank_cross_section([("AAA", 1, 10), ("BBB", 1, 30), ("CCC", 1, 20)])
# [(1, 'AAA', 3), (1, 'BBB', 1), (1, 'CCC', 2)]
rank_cross_section([("A", 1, 5), ("B", 1, 5), ("C", 1, 1)])
# [(1, 'A', 1), (1, 'B', 1), (1, 'C', 2)]
The second example is the dense rule: A and B tie at rank 1, and C
takes rank 2 rather than rank 3.
0 <= len(rows) <= 10^5, dates and values fit comfortably in int.None contributes nothing to the output.pandas and numpy are preloaded.Run your code to check it against the sample tests. Results appear here.