Skip to content
Tech Interview Prep
Technical interview guide

DeFi Primitives (AMMs, Lending, Liquidity Pools)

The core building blocks of decentralized finance: automated market makers, lending protocols, and the liquidity pools they're built on.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v4
Editorial status
Reviewed

Scope: Uniswap v2 and v3 specifications; Aave borrowing and liquidation documentation reviewed 2026-09-04 (version-specific parameters must be checked at integration); Chainlink Data Feeds; ERC-4626; ERC-3156; OWASP SCSVS.

Overview

Curated: · Written: · Reviewed:

DeFi Primitives (AMMs, Lending, Liquidity Pools)

In one line

An AMM prices swaps from pool reserves under x·y = k, and a lending market lends against overcollateralised positions. Start from the invariant, then state the cost and the failure: what the trade loses to the curve, what the LP gives up to divergence, and what breaks first when price moves fast.

Key definitions

  • Constant product (x·y = k): price is the reserve ratio y/x. No price is stored; it moves because reserves move.
  • Price impact: execution price against spot, set by trade size relative to reserves.
  • Divergence (impermanent) loss: LP value minus hold value. In a 50/50 pool it depends only on the price ratio r.
  • Liquidation threshold: the collateral-weighted limit at which a position becomes liquidatable. Always at or above max LTV; the gap is the buffer.
  • Health factor (HF): sum of collateral × price × threshold, over sum of debt × price. Below 1.0 is liquidatable.
  • Close factor: the maximum share of debt one liquidation may repay.
  • TWAP: time-weighted average price. It raises manipulation cost by requiring the distortion to persist.

Formulas and complexity

Swap output with fee f taken on input (Uniswap v2 style, f = 0.003):

Δy=y Δx (1−f)x+Δx (1−f)\Delta y = \frac{y\,\Delta x\,(1-f)}{x + \Delta x\,(1-f)}

Divergence in a 50/50 constant-product pool, r = new price / old price:

VLPVhold=2r1+r\frac{V_{LP}}{V_{hold}} = \frac{2\sqrt{r}}{1+r}

Health factor:

HF=∑iCiPiLi∑jDjPjHF = \frac{\sum_i C_i P_i L_i}{\sum_j D_j P_j}

A swap is O(1) in the pool's arithmetic. Check version-specific parameters (fees, thresholds, bonuses) against current docs at integration.

How it works

Worked trace: 10 ETH into a 100 ETH / 200,000 USDC pool. Effective input is 9.97 ETH. Output is 200,000 − 20,000,000 / 109.97 = 18,132.2 USDC. That is 1,813.2 per ETH, 9.34% below the 2,000 spot. The curve alone costs 9.09%; the fee costs the rest. The fee stays in the pool for LPs.

yes no Swap with minOut and deadline Router reads current reserves Fee taken from input Solve x·y = k for output Output at least minOut? Reserves update, fee stays in pool Revert

Lending trace: 10 ETH at 2,000 with an 82.5% threshold and 12,000 USDC debt gives HF = 16,500 / 12,000 = 1.375. Liquidation triggers at P = 12,000 / (10 × 0.825) = 1,454.5, a 27% drop. A liquidator repays up to the close factor and receives the bonus in collateral.

When to use and when not to

SituationUseAvoid
Swapping in a volatile pairDeep reserves, trade small relative to reservesLarge size: walk the curve or split, compare external liquidity
Stable pairsStableSwap, which quotes near 1:1 while the peg holdsConstant-sum behaviour off peg, where arbitrageurs drain the pool
Concentrated LPRange you expect price to stay in, high fee volumeTrending markets that exit the range: zero fees and a fully losing asset
Oracle from a poolLong TWAP over a liquid, actively arbitraged poolThin pools, short windows, thin observation history
Collateral pricingValidated external feed, or long TWAPAny single-block spot read

Traps interviewers probe

  1. Reading spot as the oracle. Fix: a TWAP over a deep, arbitraged pool or a validated feed. Moving a $4M pool 12x costs about $35,000 in fees and flash-loan costs, which is cheap against a large borrow.
  2. Treating minOut as sandwich protection. Fix: a tight bound caps your loss at the bound; it does not stop the attacker. Add a short deadline and protected submission, and disclose relay trust.
  3. Calling HF above 1 safe. Fix: measure how fast HF can cross 1.0 from volatility, oracle cadence and collateral depth. A 27% buffer is thin for a volatile asset over a block.
  4. Accounting from balanceOf. Fix: use internal accounting or getReserves(). Anyone can donate tokens to a pair and change balanceOf, and sync() and skim() expose the same surface.
  5. Empty-vault share inflation. Fix: virtual shares or offset, a burned minimum first deposit, and rounding against the caller. One share plus a 10,000 donation let an attacker take about 5,000 from a 20,000 deposit in the worked trace.

Interview tips

Open with the invariant and one number: "constant product, so 10 ETH into this pool returns about 18,132 USDC, 9.3% under spot."

Expected follow-ups:

  • A 10x trade: walk the curve from current reserves, not the spot price.
  • A one-block move: oracle staleness, liquidation latency and collateral liquidity.
  • Who pays bad debt: socialisation, reserve or backstop, pause rules.
  • What you monitor at 3am: oracle age and divergence, HF distribution below 1.05, utilisation, bad debt, AMM depth per collateral.

Oracle checks to name: updatedAt freshness against the feed heartbeat, decimals() (USD feeds use 8, so an 8-decimal answer of 300000000000 is $3,000, and read as 1e18 WAD it is $0.0000003), a positive answer inside a sane band, and a pinned proxy address.

30-second outline:

  1. Invariant: x·y = k, price is the reserve ratio.
  2. Fee on input, output formula, impact number for the trade size.
  3. Protect the user: minOut from current state, short deadline.
  4. LP: divergence 2√r/(1+r) against fee volume over time in range.
  5. Lending: threshold buffer, HF, validated oracle, and what bad debt does.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.