KatInterview

Data-Driven Fare Policy Rule Engine

Arrays & HashingAuto-graded · 17 test cases
HardAuto-gradedArrays & Hashinghash-mapsortingrule-engine

Last updated

Evaluate stored fare and per-driver policy rules over a trip table with generic field/operator conditions and exact integer-cent sums.

A trip-audit system must report which trips and which drivers break finance-defined payout policies. Policies arrive as plain table rows — nothing about a policy may be baked into code — so the evaluator must interpret conditions generically.

Write audit_fare_policies(trips, policies, conditions) -> List[List[str]].

Input contract

All money is held as whole cents (integers), never floats, so repeated additions stay exact.

  • trips: each row is [tripId, driverId, amountCents, tripType, zoneType, zoneName]. Trip IDs are unique and every column is populated. At most 10,000 rows. amountCents is a nonnegative integer written as a string, no larger than 100,000,000.
  • policies: each row is [policyId, scope, limitCents], and policyId values are unique. scope is either "FARE" or "DRIVER_SUM". For "FARE", limitCents is the empty string and is ignored. For "DRIVER_SUM", limitCents is a nonnegative integer written as a string. At most 100 policies.
  • conditions: each row is [policyId, field, operator, value], at most 1,000 rows. All conditions listed under the same policyId are combined with AND. A policy that has no conditions matches every trip. Every condition's policyId refers to a policy that exists.
  • Allowed field names: fare_id, driver_id, amount_cents, fare_type, zone_type, zone_name.
  • When field is amount_cents, both sides are compared as integers and operator may be EQ, NE, GT, GE, LT, or LE. For every other field only EQ and NE are used, and comparison is exact and case-sensitive on the strings.
  • Input is always well formed.

Output contract

Produce one row [scope, subjectId, policyId, actualCents] per violation, where subjectId is a trip ID (for "FARE" policies) or a driver ID (for "DRIVER_SUM" policies), and actualCents is that trip's amount or the driver's filtered total, written as an integer string.

Grouping and order:

  • Policies are emitted in the order given in policies.
  • Inside a "FARE" policy, a violating trip is emitted for every trip that satisfies all its conditions, in trip input order.
  • Inside a "DRIVER_SUM" policy, first total the amountCents of that policy's matching trips separately per driver, then emit a row only for drivers whose total is strictly greater than limitCents. Within such a policy, drivers appear in ascending (lexicographic) driverId order.
  • A driver with no matching trips has total zero and is never emitted, since limits are nonnegative.

Examples

Example 1

Input: audit_fare_policies([['F1', 'D1', '1200', 'airport', 'surge', 'downtown'], ['F2', 'D1', '900', 'standard', 'normal', 'suburb'], ['F3', 'D2', '5000', 'airport', 'normal', 'downtown'], ['F4', 'D2', '300', 'standard', 'normal', 'suburb'], ['F5', 'D1', '2200', 'airport', 'surge', 'downtown']], [['R1', 'FARE', ''], ['R2', 'DRIVER_SUM', '3000']], [['R1', 'fare_type', 'EQ', 'airport'], ['R1', 'amount_cents', 'GT', '1000'], ['R2', 'zone_type', 'EQ', 'surge']])

Output: [['FARE', 'F1', 'R1', '1200'], ['FARE', 'F3', 'R1', '5000'], ['FARE', 'F5', 'R1', '2200'], ['DRIVER_SUM', 'D1', 'R2', '3400']]

Hints

Hint 1

Store each policy's conditions keyed by policyId once up front, then a trip either satisfies all of them or fails on the first mismatch.

Hint 2

Write a single matching routine that handles both numeric and string fields by choosing the comparison from the field name — do not special-case individual policies.

Hint 3

The only real branching is on scope: one pass appending trips, versus one pass accumulating integer totals into a per-driver dict followed by a sorted scan with a strict `>` test.