Code · Interview problems

Interview problems

Practical, multi-part coding questions like the ones asked in real rounds: parsing, pricing, routing, rate limiting. Each one has the full question, and every part is solved in Java.

github.com/aayushi-kunwar13/sde2-code-practice ↗

Problems · 15

Bulls and Cows

Medium

Given a secret number and a guess of the same length, return the hint "xAyB": x = digits right in value and position (bulls), y = digits right in value but in the wrong position (cows).

Hashing36 lines

Payment Parser

Easy

A payment is "paymentId,amount,Paying off: invoiceId" and invoices are "invoiceId,dueDate,amount". Return "<paymentId> pays off <amount> for <invoiceId> due on <dueDate>", or NOT_FOUND. (First attempt; the full 3-part version is question7.)

String parsing28 lines

Rank Teams by Votes

Medium

Each voter ranks all teams (a string like "ACB"). Order teams by the most 1st-place votes; break ties by 2nd-place votes, and so on; if still tied, alphabetically.

Sorting42 lines

Snake and Ladder

Medium

Model a Snake and Ladder game: an n x m board with snakes (head to tail) and ladders (start to end), players with positions, and a 1-6 dice. (Entity notes only so far.)

Simulation / LLD16 lines

Text Justification

Hard

Pack words greedily into lines of exactly maxWidth characters. Spread extra spaces as evenly as possible (extra ones go to the left gaps); the last line and one-word lines are left-justified.

Strings / Simulation43 lines

Token Bucket Rate Limiter

Medium

Implement allowRequest() for a rate limiter: a bucket holds up to capacity tokens and refills at refillPerSec; each request spends one token or is rejected.

Rate limiting / Concurrency31 lines

Currency Conversion

Medium

Convert an amount using FROM:TO:RATE pairs: direct rate, then through other currencies, then the shortest route.

3 partsGraphs (DFS / BFS)209 lines

Accept-Language Parser

Medium

Return the server-supported languages in the order an Accept-Language header prefers them, then with q= weights.

2 partsString parsing80 lines

Idempotent Request Replay

Medium

Replay stored responses for repeated idempotency keys, then with key expiry (TTL), then rejecting conflicting retries.

3 partsHashing / Caching170 lines

Volume (Tiered) Pricing

Medium

Price a quantity with all-units tiers, then graduated tiers, then find the most units a budget buys.

3 partsGreedy / Math85 lines

Flight Cost Calculation

Medium

Cheapest flight from SRC:DST:AIRLINE:COST entries: direct, one stop, then any stops with route and airlines.

4 partsGraphs198 lines

Spotting an Error Spike

Hard

Alert when a merchant has 5+ errors in 30s, then relative to successes, then with TRIGGER/RESOLVE events.

2 partsSliding window262 lines

Payment Invoice Matching

Medium

Match a payment to an invoice by memo, then by equal amount (earliest due date), then within a forgiveness amount.

3 partsString parsing / Sorting211 lines

Task / Sub-task Hierarchy

Medium

Print tasks by date with their sub-tasks underneath, using |- and \- tree prefixes.

Hashing / Sorting80 lines

Merchant Payout Scheduler

Easy

Group transactions into the next payout day for a payout interval and print the totals per day.

Sorted map52 lines