
Chunk a document for embedding
easyChunk a document for embedding
Perplexity RAG Interview Question
Before a document can be searched, Perplexity splits it into chunks small enough to embed. Consecutive chunks overlap by a few characters so that a sentence sitting on a boundary still appears whole in one of them.
Write a function chunk(text, size, overlap) that splits the text into chunks of the given size in characters, where each chunk starts further along the text than the one before it by the size minus the overlap. The last chunk may be shorter. Return an empty list for empty text. You can assume the overlap is smaller than the size.
Asked of
- AI Engineer
- ML Engineer
- Data Scientist
Example 1
Input
text = "abcdefghij", size = 4, overlap = 1
Output
["abcd", "defg", "ghij", "j"]
Example 2
Input
text = "hello world", size = 5, overlap = 0
Output
["hello", " worl", "d"]
Explanation
In the first example the step is 4 - 1 = 3, so the chunks start at positions 0, 3, 6 and 9. The chunk starting at 9 has only one character left to take.
Submit also runs 3 hidden test cases that check edge cases.
- Chunking
Similar questions
Company
Perplexity
Difficulty
easy
Topic
RAG
Language
Python
Your code
Loading Python in the background. You can start writing.