HomeInterview QuestionsImplement a RAGApplication class that initializes …

Implement a RAGApplication class that initializes a vector store, tokenizes text, and builds a vocabulary from a list of texts.

🟡 Medium Coding Mid level
1Times asked
Aug 2026Last seen
Aug 2026First seen

💡 Model Answer

The RAGApplication class should maintain an in‑memory vector store and a simple vocabulary. In the constructor, initialize empty lists for documents and embeddings. The _tokenize method uses a regex to split text into lowercase tokens. The _build_vocab method iterates over the provided texts, tokenizes each, and updates a Counter to count token frequencies. After building the Counter, convert it to a list of unique tokens sorted by frequency to form the vocabulary. For embeddings, use a pre‑trained sentence transformer (e.g., sentence-transformers/all-MiniLM-L6-v2) to encode each document into a fixed‑size vector and store it alongside the document. Complexity: tokenization is O(n m) where n is the number of documents and m is average length; building the Counter is linear in total tokens; encoding each document is O(n d) where d is embedding dimension. The resulting class can then be used to retrieve the nearest neighbors by computing cosine similarity between a query embedding and stored embeddings, returning the top‑k relevant documents. This approach keeps the implementation lightweight while demonstrating core RAG concepts.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500