AI Role: Offline RAG Pipeline with a Model Adapter
Updated
Problem statement
Build a small retrieval-augmented generation (RAG) pipeline that runs offline. The document chunks and their embedding vectors are given. A list of canned model answers, modelFixtures, stands in for a real language model.
Complete runRag with these steps:
- Score chunk
ias the dot product ofembeddings[i]andqueryEmbedding. Keep only the chunks whose score is strictly positive. - Pick at most
topKof the kept chunks, highest score first. If two scores are equal, the chunk with the smaller original index comes first. - Write one context line for each picked chunk as
[index] chunk text, whereindexis the chunk's zero-based index. Join the lines with one newline character and put no newline at the end. If no chunk is picked, the context is the empty string. modelFixturesis a flat array of strings. Read it in groups of three:[questionText, contextText, modelAnswer]. Look for the group whose first two strings equal the originalquestionand your context exactly, including case and whitespace. Return that group's answer. If no group matches, the answer is the empty string. This lookup also runs when the context is empty.
Return exactly two strings: [assembledContext, modelAnswer]. You do not need a network call, an embedding model or a language model. The given vectors and canned answers make every run give the same result.
Function
runRag(chunks: String[], embeddings: int[][], question: String, queryEmbedding: int[], topK: int, modelFixtures: String[]) → String[]Examples
Example 1
chunks = ["Tea is hot","Ice is cold","Tea and ice"]embeddings = [[2,0],[0,2],[1,1]]question = "Hot drink?"queryEmbedding = [1,0]topK = 2modelFixtures = ["Hot drink?","[0] Tea is hot\n[2] Tea and ice","Tea.","Hot drink?","[2] Tea and ice\n[0] Tea is hot","Wrong order","Other?","[0] Tea is hot\n[2] Tea and ice","Wrong question"]return = ["[0] Tea is hot\n[2] Tea and ice","Tea."]The scores are 2, 0 and 1. Chunk 1 scores zero, so it is dropped. Chunks 0 and 2 are left, and topK = 2 keeps both, highest score first. The context is [0] Tea is hot\n[2] Tea and ice. Only the first fixture group has this question and this context, so the answer is Tea. and the result is ["[0] Tea is hot\n[2] Tea and ice","Tea."].
Example 2
chunks = ["A","B","C"]embeddings = [[1],[1],[0]]question = "Pick"queryEmbedding = [1]topK = 1modelFixtures = ["Pick","[0] A","first","Pick","[1] B","second"]return = ["[0] A","first"]Chunks 0 and 1 both score 1, and chunk 2 scores zero. The smaller index wins the tie, and topK = 1 keeps only chunk 0. The context is [0] A, the matching fixture answer is first, and the result is ["[0] A","first"].
Example 3
chunks = ["Unrelated"]embeddings = [[0]]question = "Missing"queryEmbedding = [3]topK = 2modelFixtures = ["Missing","","No evidence."]return = ["","No evidence."]The only chunk scores zero, so no chunk is picked and the context is the empty string. The fixture group with the question Missing and an empty context still matches, so the model answer is No evidence.. The result is ["","No evidence."].
Constraints
0 <= chunks.length <= 200.1 <= chunks[i].length <= 200.embeddings.length == chunks.length.1 <= queryEmbedding.length <= 32.- Every row of
embeddingshas the same length asqueryEmbedding. - Every coordinate in
embeddingsandqueryEmbeddingis an integer from0through1000. 1 <= question.length <= 200.- Chunk text and the question contain only printable ASCII characters, from space through tilde.
0 <= topK <= 200.0 <= modelFixtures.length <= 60.modelFixtures.lengthis a multiple of3.- Every string in
modelFixtureshas length from0through50000. - No two fixture groups have the same pair of first and second fields.
More Apple Phone Screen questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Subarray Product Less Than KPHONE SCREEN · Seen Nov 2026
- Evaluate DivisionPHONE SCREEN · Seen Nov 2026
- Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
- Validate a BST with a Custom ComparisonPHONE SCREEN · Seen Nov 2026
- Design a Food Rating SystemPHONE SCREEN · Seen Oct 2026
- Days Until Poisoned Plants StabilizePHONE SCREEN · Seen Oct 2026
- Capture Surrounded Regions with Union-FindPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Determine the Winner of Three-Card HandsOpenAI · PHONE SCREENOpenAI interview questions
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Resource Reconciler: Plan and Apply CreatesPerplexity AI · PHONE SCREENPerplexity AI interview questions
- Deterministic Secret Santa AssignmentShopify · PHONE SCREENShopify interview questions
- Shortest-Job-First Process SchedulingByteDance · PHONE SCREENByteDance interview questions
- Longest Uninterrupted Work BlockIntuit · PHONE SCREENIntuit interview questions
- Streamed Scalar Item TripletsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Bus Station Seat AllotmentInMobi · PHONE SCREENInMobi interview questions
- Wordle Guess LoopRetool · PHONE SCREENRetool interview questions
- Resolve Visible User FeaturesStripe · PHONE SCREENStripe interview questions
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)