Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Chunking for Vector Databases

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Chunking for Vector Databases. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Wipro ai360 needs consistently sized text segments before generating embeddings for retrieval. Given unstructured text, split it into overlapping word-based chunks while preserving the original word order.

First normalize whitespace by treating consecutive spaces, tabs, and newlines as a single separator. Then create chunks containing at most max_words tokens. Each token is a whitespace-delimited word, and punctuation remains attached to its word. Consecutive chunks must share exactly overlap words whenever enough words remain. The next chunk starts max_words - overlap positions after the current chunk starts.

Return metadata for each chunk as a dictionary with:

  • text: the normalized chunk text
  • start: inclusive starting word index in the normalized token list
  • end: exclusive ending word index

Return an empty list for empty or whitespace-only input. You may assume the numeric arguments satisfy the constraints.

Constraints

  • 0 <= len(text) <= 10^6
  • 1 <= max_words <= 10^5
  • 0 <= overlap < max_words
  • Tokens are whitespace-delimited words, with punctuation preserved

Function Signature

def chunk_text(text, max_words, overlap):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output