Question bank

Interview Questions — GenAI Coding Round

24 itemsWhat the provenance labels mean

In this bank

Round: Coding (required) · Difficulty tags relative to mid-level AI/GenAI engineer Provenance: every task below is Generated practice, modelled on a Common industry pattern (GenAI roles often mix standard data-structure questions with small LLM-flavoured implementations). None is verified company-specific.

Rules for practice: narrate the approach first (clarify → brute force → better → complexity → edge cases), write code, dry-run, then state test cases. Prefer pure Python/NumPy; no frameworks.

#TaskDifficultyWhat it testsEdge cases to raiseProvenanceRelated lesson
C1Cosine similarity between two vectors, then top-k nearest from a matrix (vectorised)belowNumPy, normalisationzero vector, k > n, tiesGenerated practiceEmbeddings and similarity search
C2Token-aware chunker: split text into ≤N-token chunks with overlap, never mid-sentenceatstring handling, budget logicsentence longer than N, overlap ≥ N, empty inputGenerated practiceRAG architecture and chunking
C3Token budgeter: given system prompt, history, and retrieved chunks, fit within a context limit by dropping/truncating by priorityatgreedy selection, trade-offssingle chunk exceeds budget, reserve output tokensGenerated practiceContext engineering and memory
C4Minimal RAG pipeline: ingest(docs), retrieve(q,k), build_prompt using TF-IDF or fake embeddingsatdecomposition, interfacesduplicate docs, no results, k=0Generated practiceThe core RAG pipeline
C5Reciprocal Rank Fusion of two ranked listsatranking mathdisjoint lists, ties, weightsGenerated practiceBetter retrieval, Coding drills
C6Compute recall@k, precision@k, MRR, nDCG@k for retrieval evalatmetric correctnessno relevant docs, k > lenGenerated practiceRAG evaluation in depth
C7Retry with exponential backoff + jitter wrapper for flaky LLM/API calls (sync, then async)atresilience, idempotencynon-retryable errors, max elapsedGenerated practiceRAG security, reliability and release
C8LRU cache with TTL (building block for response/semantic cache)atdata structures O(1)expiry on read, capacity 1Generated practiceCost and latency optimization
C9Parse and validate structured LLM output (JSON) with repair/retry loopatrobustnesstrailing text, wrong types, truncationGenerated practiceReasoning models and structured outputs
C10Streaming token aggregator with stop-sequence detection across chunk boundariesstretchstateful parsingstop string split across chunksGenerated practiceNot covered as a lesson in this course
C11Simple tool-calling loop with step cap and loop detectionstretchagent control flowrepeated calls, tool errorsGenerated practiceAgentic patterns: ReAct, planning and memory
C12Rate limiter (token bucket) for per-user LLM callsatconcurrency basicsburst, clock skewGenerated practiceNot covered as a lesson in this course

Reference approach for C1 (to check yours after attempting):

Python

import numpy as np
def top_k(query: np.ndarray, M: np.ndarray, k: int):
    q = query / (np.linalg.norm(query) + 1e-12)
    Mn = M / (np.linalg.norm(M, axis=1, keepdims=True) + 1e-12)
    sims = Mn @ q
    k = min(k, len(sims))
    idx = np.argpartition(-sims, k - 1)[:k]       # O(n) selection
    idx = idx[np.argsort(-sims[idx])]             # sort only the k
    return idx, sims[idx]

Talking points: normalise once (not per query) in production; argpartition vs full sort; approximate search once brute force is too slow for your corpus (measure, do not guess a threshold).

Also keep standard DSA practice (arrays/hash maps, sliding window, heaps, graphs, DP) — GenAI interview loops often still include a general coding round (a Common industry pattern, not a guarantee).

What a strong solution covers

#Strong solution
C1Normalises once, uses a matrix product, handles zero vectors with an epsilon, clamps k, and explains partial selection versus a full sort.
C2Splits on sentence boundaries, counts tokens with the target tokenizer (not words), applies overlap by whole sentences, and says what happens to an oversized sentence.
C3Reserves output tokens first, keeps the system prompt, ranks the rest by priority, and states whether it truncates or drops a chunk that does not fit.
C4Separates ingest, retrieve and build_prompt, keeps chunk IDs for citations, and returns a clear “no results” path instead of a made-up prompt.
C5States the formula 1 / (k + rank) summed over lists with k = 60 as the usual default (see Better retrieval), uses ranks not raw scores, and handles documents missing from one list.
C6Defines each metric before coding it, handles queries with no relevant documents, and shows a tiny worked example by hand.
C7Retries only retryable errors, uses exponential backoff with jitter and a maximum elapsed time, and notes that retried calls need idempotency.
C8O(1) get and put (hash map plus ordering structure), expires on read, and says how the clock is injected for tests.
C9Parses defensively, validates against a schema, retries a bounded number of times with the error fed back, and has a fallback when output is truncated.
C10Keeps a rolling buffer so a stop string split across chunks is still found, emits only text that cannot be part of a stop string, and tests the split cases.
C11Caps steps, detects repeated (tool, arguments) pairs, returns tool errors to the model in a readable form, and stops with a useful message.
C12Refills tokens by elapsed time, allows a bounded burst, keeps one bucket per user, and discusses what a rejected call returns.