Design an in-memory payment instrument recommender with per-category policy rules

Quick Overview

Design an in-memory store that recommends a user's saved payment instruments for a purchase, using per-category policies of method limits and a UPI support flag. It tests choosing data structures for fast lookups, applying eligibility rules exactly, and building a multi-key sort with case-sensitive tie-breaks.

Design an in-memory payment instrument recommender with per-category policy rules

Company: CRED

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design an in-memory payment instrument recommender. Each spending category has a configurable policy: an ordered list of rules of the form `METHOD:MAX_AMOUNT` (for example `UPI:5000`). Earlier rules have higher priority, limits are inclusive, and a method missing from the policy is ineligible. Replacing a policy with an empty list disables the category. Users carry a `supportsUPI` flag and a set of saved instruments, each with a unique (per user) `instrumentId`, a `method`, an `issuer`, and an integer `relevance` from 0 to 10000. Updating a user preserves their instruments, and upserting an instrument replaces any existing instrument with the same ID. A recommendation for `(userId, category, amount)` returns the IDs of eligible instruments: the instrument's method must appear in the policy with `amount <= limit`, and the exact method string `UPI` additionally requires `supportsUPI = true`. Results are ordered by method position in the policy, then descending relevance, then ascending instrument ID (case-sensitive ASCII). Unknown users or categories return `[]`. The `issuer` never affects eligibility or ordering. Amounts are positive integer minor units. Implement the following methods: - `setPolicy(category: str, rules: List[str])`: replace the whole policy for the category with the ordered `METHOD:MAX_AMOUNT` rules. An empty list disables the category. - `setUser(userId: str, supportsUPI: bool)`: create the user or update their UPI support flag, preserving existing instruments. - `upsertInstrument(userId: str, instrumentId: str, method: str, issuer: str, relevance: int)`: insert or fully replace the instrument with this ID for the given user. - `recommend(userId: str, category: str, amount: int) -> List[str]`: return eligible instrument IDs ordered by policy method position, descending relevance, then ascending instrument ID. Returns `[]` for unknown users or categories. Example 1 shows priority, inclusive limits and the ID tie-break: ```text setPolicy("FOOD", ["UPI:5000", "CARD:100000", "WALLET:2000"]) setUser("u1", true) upsertInstrument("u1", "card_b", "CARD", "BANK_A", 70) upsertInstrument("u1", "upi_main", "UPI", "BANK_B", 10) upsertInstrument("u1", "card_a", "CARD", "BANK_C", 70) upsertInstrument("u1", "wallet_1", "WALLET", "BANK_D", 99) upsertInstrument("u1", "nb_1", "NETBANKING", "BANK_E", 100) recommend("u1", "FOOD", 1500) -> ["upi_main", "card_a", "card_b", "wallet_1"] recommend("u1", "FOOD", 5000) -> ["upi_main", "card_a", "card_b"] recommend("u1", "FOOD", 5001) -> ["card_a", "card_b"] ``` `UPI` comes first because it is the first rule, even though `upi_main` has the lowest relevance. The two cards tie on relevance 70, so their IDs decide the order. `nb_1` never appears because `NETBANKING` is not in the policy. At 5000 the `UPI` limit still holds because limits are inclusive, while `WALLET` (limit 2000) drops out; at 5001 `UPI` drops out too. Example 2 shows the UPI flag, user updates, replacement, case-sensitive ordering and the empty results: ```text setPolicy("TRAVEL", ["CARD:50000", "UPI:20000"]) setUser("u2", false) upsertInstrument("u2", "upi_x", "UPI", "BANK_A", 90) upsertInstrument("u2", "card_1", "CARD", "BANK_B", 20) upsertInstrument("u2", "Card_2", "CARD", "BANK_C", 20) recommend("u2", "TRAVEL", 10000) -> ["Card_2", "card_1"] setUser("u2", true) recommend("u2", "TRAVEL", 10000) -> ["Card_2", "card_1", "upi_x"] upsertInstrument("u2", "card_1", "CARD", "BANK_B", 60) recommend("u2", "TRAVEL", 10000) -> ["card_1", "Card_2", "upi_x"] recommend("nobody", "TRAVEL", 10000) -> [] recommend("u2", "GROCERY", 10000) -> [] setPolicy("TRAVEL", []) recommend("u2", "TRAVEL", 10000) -> [] ``` `upi_x` is excluded until the user's flag turns on, and the update keeps all three instruments. `Card_2` sorts before `card_1` on a relevance tie because uppercase `C` (code 67) comes before lowercase `c` (code 99). Raising `card_1` to relevance 60 moves it ahead. An unknown user, an unknown category and a disabled category all return `[]`. ```hint Lookups first Think about how to store a policy so that checking one instrument's method gives you both its priority and its limit in a single lookup. ``` ```hint Ordering pitfall The ordering has three fields and one of them is descending. Build it as one sort key, and check that your language compares IDs by raw character codes and not by a locale-aware rule. ``` ### Constraints & Assumptions - At most 20,000 operations in total. - At most 1,000 instruments per user and 20 methods per policy. - `1 <= amount <= 10^9` and `0 <= MAX_AMOUNT <= 10^9`. - `0 <= relevance <= 10000`. - All identifiers contain only ASCII letters, digits or underscores, and ordering is case-sensitive ASCII. - Each rule has exactly one colon, and method names within a policy are unique. - `upsertInstrument` is only called for a user that already exists. ### Clarifying Questions to Ask - Is the `UPI` check an exact, case-sensitive match on the method string, so a method named `upi` is treated like any other method? - Should a category disabled with an empty list behave exactly like a category that was never set? - Will calls come from several threads at once, or can the store assume one caller at a time? ### What a Strong Answer Covers - Data structures for policies, users and per-user instruments, with the cost of each lookup. - The eligibility filter: method present in the policy, inclusive limit, and the UPI flag applied only to the exact method `UPI`. - One sort key that gives method position ascending, relevance descending and instrument ID ascending in case-sensitive ASCII order. - The empty-result cases (unknown user, unknown category, disabled category) and keeping instruments when a user is updated. - Time and space complexity for each method under the stated limits. ### Follow-up Questions - With at most 20 methods per policy, how would you avoid sorting all of a user's instruments on every `recommend` call? - How would you add a way to delete an instrument or a user, and what should `recommend` return for a deleted user? - How would you make the store safe when several threads call `upsertInstrument` and `recommend` concurrently? - If the product later wanted the issuer to boost certain instruments, where would that rule plug into the current design?

Overview: Design an in-memory store that recommends a user's saved payment instruments for a purchase, using per-category policies of method limits and a UPI support flag. It tests choosing data structures for fast lookups, applying eligibility rules exactly, and building a multi-key sort with case-sensitive tie-breaks.

CRED logo
CRED
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design an in-memory payment instrument recommender. Each spending category has a configurable policy: an ordered list of rules of the form METHOD:MAX_AMOUNT (for example UPI:5000). Earlier rules have higher priority, limits are inclusive, and a method missing from the policy is ineligible. Replacing a policy with an empty list disables the category.

Users carry a supportsUPI flag and a set of saved instruments, each with a unique (per user) instrumentId, a method, an issuer, and an integer relevance from 0 to 10000. Updating a user preserves their instruments, and upserting an instrument replaces any existing instrument with the same ID.

A recommendation for (userId, category, amount) returns the IDs of eligible instruments: the instrument's method must appear in the policy with amount <= limit, and the exact method string UPI additionally requires supportsUPI = true. Results are ordered by method position in the policy, then descending relevance, then ascending instrument ID (case-sensitive ASCII). Unknown users or categories return []. The issuer never affects eligibility or ordering. Amounts are positive integer minor units.

Implement the following methods:

  • setPolicy(category: str, rules: List[str]) : replace the whole policy for the category with the ordered METHOD:MAX_AMOUNT rules. An empty list disables the category.
  • setUser(userId: str, supportsUPI: bool) : create the user or update their UPI support flag, preserving existing instruments.
  • upsertInstrument(userId: str, instrumentId: str, method: str, issuer: str, relevance: int) : insert or fully replace the instrument with this ID for the given user.
  • recommend(userId: str, category: str, amount: int) -> List[str] : return eligible instrument IDs ordered by policy method position, descending relevance, then ascending instrument ID. Returns [] for unknown users or categories.

Example 1 shows priority, inclusive limits and the ID tie-break:

setPolicy("FOOD", ["UPI:5000", "CARD:100000", "WALLET:2000"])
setUser("u1", true)
upsertInstrument("u1", "card_b", "CARD", "BANK_A", 70)
upsertInstrument("u1", "upi_main", "UPI", "BANK_B", 10)
upsertInstrument("u1", "card_a", "CARD", "BANK_C", 70)
upsertInstrument("u1", "wallet_1", "WALLET", "BANK_D", 99)
upsertInstrument("u1", "nb_1", "NETBANKING", "BANK_E", 100)
recommend("u1", "FOOD", 1500)   -> ["upi_main", "card_a", "card_b", "wallet_1"]
recommend("u1", "FOOD", 5000)   -> ["upi_main", "card_a", "card_b"]
recommend("u1", "FOOD", 5001)   -> ["card_a", "card_b"]

UPI comes first because it is the first rule, even though upi_main has the lowest relevance. The two cards tie on relevance 70, so their IDs decide the order. nb_1 never appears because NETBANKING is not in the policy. At 5000 the UPI limit still holds because limits are inclusive, while WALLET (limit 2000) drops out; at 5001 UPI drops out too.

Example 2 shows the UPI flag, user updates, replacement, case-sensitive ordering and the empty results:

setPolicy("TRAVEL", ["CARD:50000", "UPI:20000"])
setUser("u2", false)
upsertInstrument("u2", "upi_x", "UPI", "BANK_A", 90)
upsertInstrument("u2", "card_1", "CARD", "BANK_B", 20)
upsertInstrument("u2", "Card_2", "CARD", "BANK_C", 20)
recommend("u2", "TRAVEL", 10000)       -> ["Card_2", "card_1"]
setUser("u2", true)
recommend("u2", "TRAVEL", 10000)       -> ["Card_2", "card_1", "upi_x"]
upsertInstrument("u2", "card_1", "CARD", "BANK_B", 60)
recommend("u2", "TRAVEL", 10000)       -> ["card_1", "Card_2", "upi_x"]
recommend("nobody", "TRAVEL", 10000)   -> []
recommend("u2", "GROCERY", 10000)      -> []
setPolicy("TRAVEL", [])
recommend("u2", "TRAVEL", 10000)       -> []

upi_x is excluded until the user's flag turns on, and the update keeps all three instruments. Card_2 sorts before card_1 on a relevance tie because uppercase C (code 67) comes before lowercase c (code 99). Raising card_1 to relevance 60 moves it ahead. An unknown user, an unknown category and a disabled category all return [].

Constraints & Assumptions

  • At most 20,000 operations in total.
  • At most 1,000 instruments per user and 20 methods per policy.
  • 1 <= amount <= 10^9 and 0 <= MAX_AMOUNT <= 10^9 .
  • 0 <= relevance <= 10000 .
  • All identifiers contain only ASCII letters, digits or underscores, and ordering is case-sensitive ASCII.
  • Each rule has exactly one colon, and method names within a policy are unique.
  • upsertInstrument is only called for a user that already exists.

Clarifying Questions to Ask Guidance

  • Is the UPI check an exact, case-sensitive match on the method string, so a method named upi is treated like any other method?
  • Should a category disabled with an empty list behave exactly like a category that was never set?
  • Will calls come from several threads at once, or can the store assume one caller at a time?

What a Strong Answer Covers Guidance

  • Data structures for policies, users and per-user instruments, with the cost of each lookup.
  • The eligibility filter: method present in the policy, inclusive limit, and the UPI flag applied only to the exact method UPI .
  • One sort key that gives method position ascending, relevance descending and instrument ID ascending in case-sensitive ASCII order.
  • The empty-result cases (unknown user, unknown category, disabled category) and keeping instruments when a user is updated.
  • Time and space complexity for each method under the stated limits.

Follow-up Questions Guidance

  • With at most 20 methods per policy, how would you avoid sorting all of a user's instruments on every recommend call?
  • How would you add a way to delete an instrument or a user, and what should recommend return for a deleted user?
  • How would you make the store safe when several threads call upsertInstrument and recommend concurrently?
  • If the product later wanted the issuer to boost certain instruments, where would that rule plug into the current design?