skip to content

Reinforcement Learning

Learning by acting: an agent explores an environment, collects delayed reward and improves its policy with Q-learning or policy gradients. Interviewers check you can tell it from supervised learning.

on this pageshow

explore

questions

page 1 of 2

Why does an epsilon-greedy reinforcement learning agent deliberately take non-greedy actions?

level: juniorimportance: must knowfreq 78%

answer

  1. the values are estimates, not truths
  2. one unlucky sample can be permanent
  3. the policy chooses its own training data
  4. epsilon is the random-action probability
  5. the random draw can re-pick the greedy action

basics

~20 s

An agent's action values are only estimates. With probability epsilon it acts randomly so it keeps sampling actions its estimates currently undervalue, which corrects an unlucky early estimate instead of locking onto the first action that looked good.

solid answer

~40 s

Epsilon-greedy takes the action with the highest current estimated value with probability `1 - epsilon`, and a uniformly random action with probability `epsilon`. The random branch exists because those values are estimates built from a few noisy samples: if an action's first returns happen to be poor, a purely greedy agent stops selecting it, and the estimate is then frozen wrong forever because the only evidence that would fix it is evidence the agent will never collect. Exploration keeps every action under revision. The price is real: epsilon of the time you knowingly take an action you believe is worse. One detail worth stating — the greedy action's total probability is `1 - epsilon + epsilon/|A|`, since the uniform branch can select it too.

code

python · 19 lines
python
import random

q = {"left": 1.2, "right": 1.5, "stay": 0.0}   # current value estimates
epsilon = 0.1

def select(values, eps):
    if random.random() < eps:
        return random.choice(list(values))            # explore: uniform over all actions
    best = max(values.values())
    return random.choice([a for a, v in values.items() if v == best])  # exploit

random.seed(0)
counts = {a: 0 for a in q}
for _ in range(10000):
    counts[select(q, epsilon)] += 1

for a in q:
    print(a, counts[a] / 10000)
# left 0.0345 / right 0.9327 / stay 0.0328

go deeper

for a junior

Be ready to state the rule in one line: with probability epsilon act uniformly at random, otherwise take the highest-valued action. Then say why, in one more line: the values are estimates, and a bad early sample would otherwise never be corrected.

for a middle

An interviewer expects the mechanics. Know that the greedy action's total probability is 1 - epsilon + epsilon over the number of actions, and be able to explain why uniform exploration wastes budget on options already ruled out.

for a senior

Show you have actually tuned this. Talk about where you started epsilon, how you knew exploration was still buying information, and why you evaluate a greedy rollout separately from the behaviour the agent shows while training.

for a principal

Own the framing that exploration is a spend, not a hyperparameter detail. Every exploratory step is paid in real reward, and the team should be able to say what that budget buys and when it stops being worth paying.

### What the agent actually knows A reinforcement-learning agent picks actions from an *estimate* of how good each action is. Write it `Q(s, a)`: the agent's current guess at the total discounted reward it will collect if it takes action `a` in state `s` and behaves sensibly afterwards. The word that matters is *guess*. Before training, every entry is whatever you initialised it to. After a few episodes, each entry summarises a handful of noisy samples. The **greedy** action in a state is `argmax_a Q(s, a)` — the one with the highest current estimate. A purely greedy agent always takes it. ### Why pure greed fails Two actions are available in some state. A has a true expected value of 5, B a true expected value of 8, and the rewards are noisy. The agent happens to try B first and gets 2. Its estimate of B drops toward 2. It then tries A and gets 5. Now `Q(A) = 5` beats `Q(B) = 2`, so a greedy agent takes A — and taking A only ever updates `Q(A)`. `Q(B)` stays at 2 for the rest of training. The agent has committed to the worse action on the strength of one unlucky sample, and no amount of extra training rescues it. The deep point to make in an interview is not that greedy is impatient. It is that **the agent's policy decides what data the agent will ever see**. In supervised learning the dataset is handed to you; here the choice of action is also the choice of training example, so a wrong estimate can protect itself indefinitely. Exploration is how you break that loop. ### The rule itself At each decision point, draw a uniform random number. If it is below `epsilon`, pick an action uniformly at random from the available set; otherwise take the greedy action. Two consequences follow. First, **every action in a visited state has selection probability at least `epsilon/|A|`**, where `|A|` is the number of actions. That positive floor is the whole mechanism: an action that keeps being selected keeps being updated, so its estimate can recover from a bad start. Second, the greedy action's total probability is `1 - epsilon + epsilon/|A|`, not `1 - epsilon`, because the random branch can land on it as well. With `epsilon = 0.1` and four actions that is 0.925. ### What exploration costs Every exploratory step is a step where you knowingly take an action you believe is worse. The expected immediate cost per step is roughly `epsilon` times the average gap between the greedy action's value and a random action's value. That is a real, ongoing charge against the reward the agent collects, which is why the parameter is usually reduced as the estimates firm up rather than left fixed. ### Strengths and weaknesses of the uniform scheme It is trivially simple: one knob, no assumptions about the reward distribution, and it works with any way of representing values. Its weakness is that it is **undirected**. An action that has been sampled a thousand times and is clearly bad receives exactly the same `epsilon/|A|` share as one that has never been tried, so in a large action set most exploratory steps buy almost no information. Schemes that weight exploration by how uncertain or how novel an option is are strictly better on that axis, at the cost of extra bookkeeping. The second weakness only appears in sequential problems: because the coin is re-flipped every step, exploratory behaviour is a random walk rather than a purposeful trip to an unexplored region of the state space. ### How it is used in practice `epsilon` typically starts high — often at 1.0, since the value table is meaningless at that point — and is annealed downward as estimates stabilise, usually to a small floor rather than exactly zero so that a change in the environment can still be noticed. Behaviour during training and behaviour at evaluation are separated: you evaluate the greedy policy, because the exploratory steps depress the measured return and would make a good agent look mediocre. ### What an interviewer is listening for The phrase "the values are estimates", the self-confirming-data argument, the fact that exploration is paid for in real reward, and awareness that `epsilon` is normally a schedule rather than a constant.

  • How do you compute the total probability that an epsilon-greedy agent takes the greedy action?
    `1 - epsilon + epsilon/|A|`, where `|A|` is the number of available actions. The greedy branch fires with probability `1 - epsilon`, and the uniform branch, which fires with probability `epsilon`, still picks the greedy action one time in `|A|`. With `epsilon = 0.1` and four actions that is 0.925, not 0.9.
  • What is the main weakness of uniform random exploration?
    It is undirected: it spends the same budget on an action it has strong evidence against as on one it has never tried. In a large action set most exploratory steps therefore buy almost no information. Exploration weighted by uncertainty or novelty targets the same budget at the options where the estimate is actually in doubt.
  • Does exploration still make sense once an agent's values have converged?
    Only if the environment can change. In a stationary problem, exploring after convergence is pure cost and epsilon should go towards zero. In a drifting environment a small permanent floor is what lets the agent notice that a previously poor action has become good — you accept a permanent trickle of regret in exchange for staying current.

A commuter who tries one route, hits a crash on day one, and never drives it again has priced that road on a single bad morning. Occasionally taking it anyway is the only way to find out it is normally the fastest.

saying these in an interview costs you the question

  • Says exploration just adds noise, with no purpose given
  • Claims a purely greedy agent still reaches the optimal policy
  • Thinks epsilon is the probability of taking the best action
  • Believes exploration is free because rewards get averaged
  • Cannot say what exploration costs in reward terms

context

open as a page

What are the five components of a Markov decision process?

level: juniorimportance: must knowfreq 72%

basics

~20 s

An MDP is written as states, actions, a transition function giving the probability of the next state, a reward function, and a discount factor gamma. Together they define a sequential decision problem an agent solves by choosing actions.

open as a page

Value iteration needs a known model — what exactly must that model give you?

level: juniorimportance: must knowfreq 50%

basics

~20 s

For every state and action, the model must give the probability of each possible next state and the expected reward. Value iteration averages over all successors in every update, so it plans inside the model rather than learning from experience.

open as a page

Why do policy gradient methods suit continuous action spaces better than value-based control?

level: juniorimportance: must knowfreq 66%

basics

~20 s

Policy gradient methods tune the policy's parameters directly, so an action is sampled from a distribution. Value-based control has to maximise a value estimate over all actions at every step, which is intractable when the action is a real-valued vector.

open as a page

What is the difference between V(s) and Q(s,a) in reinforcement learning?

level: juniorimportance: must knowfreq 80%

basics

~20 s

V(s) is the expected return from state s when the agent follows its policy from there on. Q(s,a) is the expected return from taking action a in state s first, then following the same policy.

open as a page

What does the discount factor gamma control in a reinforcement learning agent's return?

level: middleimportance: must knowfreq 64%

basics

~20 s

Gamma sets how much a reward arriving k steps later is worth now: it is weighted by gamma^k. Low gamma makes the agent short-sighted, high gamma far-sighted, and gamma below 1 keeps the total finite in a task that never ends.

open as a page

How does policy iteration alternate evaluation and improvement to reach an optimal policy?

level: middleimportance: must knowfreq 65%

basics

~20 s

Policy iteration repeats two steps. Evaluation sweeps every state, replacing its value with the expected return of the current policy. Improvement then rebuilds the policy greedily from those values; when no action changes, the policy is optimal.

open as a page

How does REINFORCE turn sampled episode returns into a gradient on the policy's parameters?

level: middleimportance: must knowfreq 72%

basics

~20 s

REINFORCE weights the gradient of each action's log-probability by the return that followed it: theta <- theta + lr * G_t * grad log pi(a_t | s_t). Actions from high-return episodes get more probable. The reward itself is never differentiated.

open as a page

How do the Q-learning and SARSA update rules differ, and which one is off-policy?

level: middleimportance: must knowfreq 65%

basics

~20 s

Q-learning's target uses the maximum action value in the next state, so it learns the greedy policy while behaving some other way, which makes it off-policy. SARSA's target uses the action actually taken next, so it evaluates the exploring policy itself: on-policy.

open as a page

How does the one-step TD(0) update revise a state-value estimate?

level: middleimportance: must knowfreq 74%

basics

~20 s

After one transition, TD(0) moves the estimate a fraction of the way toward the observed reward plus the discounted estimate of the next state: V(s) <- V(s) + alpha * [r + gamma * V(s') - V(s)]. The bracketed quantity is the TD error.

open as a page

How does the Bellman optimality equation differ from the Bellman expectation equation?

level: middleimportance: must knowfreq 66%

basics

~20 s

Both split a value into immediate reward plus the discounted value of the next state. The expectation equation averages over the actions a fixed policy takes and is linear in the values; the optimality equation takes the maximum over actions, which makes it nonlinear and defines the best achievable value.

open as a page

When does a delayed payoff justify reinforcement learning over supervised next-step prediction?

level: middleimportance: must knowfreq 58%

basics

~20 s

Reinforcement learning earns its cost when the only outcome you can measure arrives after a long sequence of decisions, so no per-decision target exists. If you can write an honest label for each decision, supervised learning is cheaper and safer.

open as a page

What separates model-free from model-based reinforcement learning?

level: juniorimportance: should knowfreq 62%

basics

~20 s

A model-based agent has (or learns) the environment's transition and reward dynamics and can plan against them. A model-free agent never builds those dynamics; it estimates values or a policy directly from sampled experience. Temporal-difference learning is model-free.

open as a page

What goes wrong when a Q-learning agent's epsilon decays to near zero after 1% of training?

level: middleimportance: should knowfreq 55%

basics

~20 s

The agent commits to whatever policy looked best while its value estimates were still mostly noise. States it stopped visiting keep their initial values, so the learning curve flattens early on a suboptimal policy that more exploration would have escaped.

open as a page

What is the difference between value iteration and policy iteration on the same MDP?

level: middleimportance: should knowfreq 58%

basics

~20 s

Policy iteration evaluates the current policy to convergence, then makes it greedy, repeats. Value iteration folds both into one sweep that takes the maximum over actions. Both reach the same optimal values, trading cost per round against number of rounds.

open as a page

In REINFORCE, why does subtracting a baseline from the return cut gradient variance?

level: middleimportance: should knowfreq 57%

basics

~10 s

Weighting by return minus a baseline centres the weights, so better-than-average actions are pushed up and worse-than-average ones down, instead of everything rising. Any baseline independent of the chosen action leaves the gradient unbiased.

open as a page

Why is a Monte-Carlo return unbiased while a one-step TD target is biased?

level: middleimportance: should knowfreq 55%

basics

~20 s

A first-visit Monte-Carlo return is an actual sampled return, so on average it equals the true value — unbiased, but noisy because it accumulates every random reward to the end of the episode. A TD target uses the current estimate of the next state, which is wrong early on, so it is biased but far lower variance.

open as a page

In reinforcement learning, why does the cost of one environment interaction decide feasibility?

level: middleimportance: should knowfreq 50%

basics

~20 s

Trial-and-error learning needs enormous numbers of episodes, so reinforcement learning is feasible only where one episode is cheap, fast and safe. A simulator can yield a million overnight; an agent whose episode is a fiscal quarter yields four a year.

open as a page

Why does an exploratory action cost more when it also changes the agent's next state?

level: seniorimportance: should knowfreq 44%

basics

~20 s

You lose more than one step's reward: a random action moves the agent into a new state, so the true cost is the return given up from there — the whole episode, if that state is unrecoverable.

open as a page

How do you check that a patient's chart is a Markov state for a discharge decision?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Ask whether earlier history would change the action you take. If two patients with identical charts but opposite three-day trends need different decisions, the chart is not a Markov state, so fold trend summaries into it.

open as a page

Why can a reinforcement learning agent maximise its reward and still fail the task?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Because the reward is a proxy for what you want, and the agent optimises the proxy literally. It will find whatever loophole scores highest, so a rising reward curve is evidence the reward is being maximised, not that the task is being done.

open as a page

Why can acting greedily with respect to a policy's value function never make it worse?

level: seniorimportance: should knowfreq 34%

basics

~20 s

In every state the greedy action is worth at least as much as the current policy's action, and that advantage applies again at the next step. Unrolling it shows the greedy policy's return dominates everywhere — the policy improvement theorem.

open as a page

Why must a REINFORCE run discard its collected trajectories after a single gradient update?

level: seniorimportance: should knowfreq 43%

basics

~20 s

The policy gradient is an expectation over trajectories drawn from the current policy. One update moves the policy, so the batch now comes from a different distribution and estimates the wrong expectation. Every step needs fresh interaction.

open as a page

Why does SARSA learn a safe inland route while Q-learning hugs the cliff edge under the same epsilon-greedy exploration?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Q-learning bootstraps from the greedy next action, so its values describe an agent that never steps off the cliff, and the short edge path looks best. SARSA bootstraps from the exploratory action actually taken, so occasional random falls depress the edge cells' values.

open as a page

How does a value function assign credit when the reward arrives only at the end of a long sequence?

level: seniorimportance: should knowfreq 42%

basics

~20 s

An early state's value is the expected discounted total of all reward that follows it, so an action paying nothing now still scores highly if it makes the payoff likelier later. Credit flows backwards through the Bellman recursion, one step at a time.

open as a page

In offline RL on logged ICU sepsis dosing records, what makes the learned policy untrustworthy?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Trained only on logged decisions, an offline policy extrapolates the value of doses clinicians rarely gave, and maximisation selects exactly those over-optimistic estimates. You also cannot test it without acting, and the unrecorded reasons behind each dose confound the data.

open as a page

How do optimistic initial Q-values make an agent explore without any random actions?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

Set every action's initial value above anything actually achievable and a purely greedy agent still explores: each action it tries returns less than promised, its estimate falls, and a still-untried action becomes the new maximum. Exploration is driven by disappointment.

open as a page

In reinforcement learning, how do you add intermediate rewards without changing the optimal policy?

level: seniorimportance: nice to knowfreq 32%

basics

~20 s

Use potential-based shaping: add gamma times a potential of the next state minus the potential of the current state. Because these terms cancel along any trajectory, the optimal policy is provably unchanged while the agent still gets useful intermediate signal.

open as a page

Value iteration planned an optimal policy from estimated transition probabilities — what can go wrong?

level: seniorimportance: nice to knowfreq 26%

basics

~20 s

The policy is exactly optimal for the model you supplied, not for the world. Errors in the transition probabilities compound over the discounted horizon, and the planner actively exploits any transition whose probability or reward was estimated too optimistically.

open as a page

How do you choose n in n-step TD returns when the reward arrives far in the future?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

Pick n on the delay between action and reward. Small n gives low-variance but myopic targets that propagate credit one state per pass; large n propagates credit fast but carries the noise of a long trajectory. Tune n empirically; intermediate values usually beat both extremes.

open as a page

showing 1–30 of 33