NLP Fundamentals Practice
7 interview questions: TF-IDF, perplexity, BPE, BLEU, NER F1 and hybrid search.
How to use this page
Question 1 · Easy · preprocessing
A teammate adds stop-word removal, stemming and lowercasing to a review sentiment classifier. Accuracy drops from 91% to 84%, mostly on reviews like “the food was not bad”. What went wrong, and what would you do?
Say your approach out loud first.
Answer: standard stop-word lists contain negations (“not”, “no”, “never”), so “not bad” became “bad” and the label flipped
- Look at the errors first: if they cluster on negated reviews, check the stop-word list. NLTK's and scikit-learn's English lists include “not” and “no”.
- Fix: keep negations (custom stop list), or skip stop-word removal entirely: with TF-IDF the common words are already down-weighted, and with Transformers you don't remove anything.
- Add bigrams (“not bad”) so the model sees the negation and the word together.
Follow-up: Is stemming ever harmful? Yes: it merges words with different meanings (“university” and “universe” both stem to “univers” with Porter) and produces non-words, which hurts when you need readable features.
Question 2 · Easy · TF-IDF
You have 400 reviews. “the” appears in all 400; “paneer” appears in 4. Using idf = ln(N / df), what are their idf values, and what does that mean for search?
Say your approach out loud first.
Answer: idf(the) = ln(400/400) = 0; idf(paneer) = ln(400/4) = ln 100 ≈ 4.61
- A word in every document carries no information about which document you want, so its idf, and therefore its TF-IDF weight, is 0.
- A rare word like “paneer” gets a high weight, so a query containing it is matched mainly to the few documents that mention it.
- Libraries add smoothing (scikit-learn uses ln((1 + N) / (1 + df)) + 1 by default), so exact numbers differ but the ordering is the same.
Follow-up: Why do we L2-normalise TF-IDF vectors before cosine similarity? So long reviews don't score higher just because they repeat words more; cosine then compares the direction of the vectors.
Question 3 · Medium · perplexity
A bigram model scores “<s> pizza cold </s>” with P(pizza | <s>) = 0.5, P(cold | pizza) = 0.25, P(</s> | cold) = 0.5. What is the perplexity over the 3 predicted tokens?
Say your approach out loud first.
Answer: PP = (0.5 × 0.25 × 0.5)^(−1/3) = 0.0625^(−1/3) ≈ 2.52
- Probability of the sequence: 0.5 × 0.25 × 0.5 = 0.0625.
- Perplexity is the inverse probability normalised by the number of predictions N = 3: 0.0625^(−1/3) ≈ 2.52. Equivalently exp(−(ln 0.5 + ln 0.25 + ln 0.5) / 3).
- Reading: on average the model is as uncertain as choosing among about 2.5 equally likely words at each step.
Follow-up: What happens if one of the bigrams was never seen and the model is unsmoothed? That factor is 0, the product is 0 and perplexity is infinite: that's why n-gram models need smoothing such as Kneser–Ney.
Question 4 · Medium · BPE
Train BPE on the word counts hug ×10, pug ×5, pun ×12, bun ×4, hugs ×5, starting from characters. What are the first two merges, and how is “bug” tokenised afterwards?
Say your approach out loud first.
Answer: merge 1: u + g → “ug” (count 20); merge 2: u + n → “un” (count 16); “bug” → b + ug
- Count adjacent pairs weighted by word frequency: u-g appears in hug, pug, hugs = 10 + 5 + 5 = 20; p-u = 17; u-n = 16; h-u = 15. Merge u-g.
- Recount after the merge: u-n = 12 + 4 = 16, h-ug = 15, p-u = 12. Merge u-n.
- To tokenise “bug”, apply the merges in order: b u g → b ug. A word with an unseen character like “mug” would hit an unknown token, which byte-level BPE avoids.
Follow-up: How does WordPiece choose differently? It scores a pair by count(pair) / (count(first) × count(second)), preferring pairs whose parts rarely appear apart, rather than raw frequency (Hugging Face course, ch. 6).
Question 5 · Medium · BLEU
Reference: “the cat is on the mat”. Candidate: “the the the the the the the”. What is the unigram precision, and BLEU's modified (clipped) unigram precision?
Say your approach out loud first.
Answer: plain precision 7/7 = 1.0; clipped precision 2/7 ≈ 0.29
- Every candidate word appears in the reference, so plain precision is 7/7, a perfect score for nonsense.
- BLEU clips each word's count at the maximum number of times it appears in any reference: “the” appears twice, so only 2 of the 7 count: 2/7.
- This is the example from the original BLEU paper (Papineni et al., 2002).
Follow-up: Your new summariser raises ROUGE-L from 0.41 to 0.44 but users like it less. How? ROUGE rewards word overlap, not faithfulness or readability; check for copied but irrelevant sentences, and validate with human ratings or an LLM judge checked against humans.
Question 6 · Medium · NER F1
A sentence has 4 gold entities. Your NER model predicts 5 entities; 3 of them match a gold entity exactly (same span and type). What are entity-level precision, recall and F1?
Say your approach out loud first.
Answer: P = 3/5 = 0.60, R = 3/4 = 0.75, F1 = 2 × 0.60 × 0.75 / 1.35 ≈ 0.67
- Precision = correct predictions / all predictions = 3/5.
- Recall = correct predictions / gold entities = 3/4.
- F1 is their harmonic mean, 0.67. A prediction with the right type but one token off counts as both a false positive and a false negative under exact-match scoring (CoNLL).
Follow-up: Why not report token accuracy? Most tokens are O (outside any entity), so a model that predicts all O can still score high token accuracy while finding nothing.
Question 7 · Hard · hybrid retrieval
Your RAG search uses dense embeddings. Users report that queries with order IDs like “ORD-48213” and dish names like “Bawarchi biryani” return the wrong documents, while paraphrased questions work well. What's happening, and how do you fix it? With reciprocal rank fusion (k = 60), document A is ranked 1st by BM25 and 10th by dense; document B is 3rd and 2nd. Which ranks higher?
Say your approach out loud first.
Answer: dense embeddings blur exact rare tokens; add BM25 and fuse with RRF. B wins: 1/63 + 1/62 ≈ 0.0320 vs A's 1/61 + 1/70 ≈ 0.0307
- Dense retrievers map text to meaning; IDs, codes and rare names are split into subword pieces and land near similar-looking strings, so exact matches are lost. BM25 matches exact terms and weights rare ones highly (high idf).
- Run BM25 and dense retrieval in parallel and fuse the ranked lists with reciprocal rank fusion, which needs no score calibration: score(d) = Σ 1 / (k + rank).
- A: 1/61 + 1/70 ≈ 0.0164 + 0.0143 = 0.0307. B: 1/63 + 1/62 ≈ 0.0159 + 0.0161 = 0.0320. B, ranked well by both, beats A, ranked top by only one.
- Then measure recall@k and MRR on a labelled query set that includes both ID lookups and paraphrased questions, and optionally add a cross-encoder re-ranker on the fused top 20–50.
Follow-up: When would you route instead of fuse? If a query matches a strict pattern (an order ID regex), skip retrieval and do a direct database lookup: it's exact, faster and cheaper.
More practice and sources
7 interview questions, easy to hard · answers hidden until you click · checked 29 Oct 2026
Built from the lessons in this track and their sources: Jurafsky & Martin, SLP3 (ch. 3, 4, 8, 11); Sennrich et al. (2016) and the Hugging Face course BPE example; Papineni et al. (2002) for BLEU clipping; Cormack et al. (2009) for RRF. Review: Preprocessing, TF-IDF, N-gram LMs, Tokenisation, NER, Retrieval, Evaluation.