Mid-level interview questionAsked at Microsoft · interview write-up
How would you measure an LLM's answer quality and detect hallucinations against the context it was given? Then implement the evaluation component.
Model answer
Quality is task-specific: reference answers where they exist, a rubric judge, human spot checks. Hallucination against supplied context is faithfulness: split the answer into claims and check each is supported by the context.
import re
from dataclasses import dataclass, field
@dataclass
class Result:
score: float
unsupported: list = field(default_factory=list)
def claims(answer):
parts = re.split(r"(?<=[.!?])\s+", answer)
return [p.strip() for p in parts if p.strip()]
def faithfulness(answer, context, supports):
# supports(context, claim) -> bool: an NLI
# model or a judge prompt
cs = claims(answer)
if not cs:
return Result(1.0)
bad = [c for c in cs if not supports(context, c)]
return Result(1 - len(bad) / len(cs), bad)
Interviewers look for claim-level scoring, a pluggable judge validated against human labels, and the empty case handled.
Practise the rest
11 Hallucination questions, 492 in all, each with a model answer. Free.
More from Microsoft interview questions.
More Hallucination interview questions
- What is a hallucination, and why can a next-token predictor produce one so fluently?
- GPT-2 small completes 'The capital of Australia is the city of' with Sydney. Walk me through why a model does that.
- Why do long generated answers leave more room for hallucination than one-word answers?
- You want to automatically flag answers that are probably wrong, using the model's token probabilities. How would you design and validate that?
- You're shipping a support bot that answers from your company's policy documents. What do you put in place to reduce hallucination, and how do you evaluate it?
- Design how a chatbot falls back when it doesn't know the answer, covering uncertainty, retrieval and tools.