FastPrepSliding-Window Text Chunking
Problems/Sliding-Window Text Chunking

Sliding-Window Text Chunking

SAP logoSAP● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a SAP intern onsite interview. The report describes a text-chunking question followed by a sliding-window implementation. It does not show the text unit, the function signature, the edge cases or the output format. In our version, the input is already split into words. Overlapping windows move forward by chunkSize - overlap. A final partial chunk is kept if it is not empty. Our version is about 70% the same as the reported question.

You are given an array of words words, a positive integer chunkSize, and an integer overlap satisfying 0 <= overlap < chunkSize.

Starting at index 0, create a chunk containing up to chunkSize consecutive words. Then advance the start index by chunkSize - overlap and repeat while the start index is within the array.

Return all chunks in source order. Keep the final non-empty chunk even if it contains fewer than chunkSize words. If words is empty, return an empty array.

Function

chunkText(words: String[], chunkSize: int, overlap: int) → String[][]

Examples

Example 1

words = ["a","b","c","d","e"]chunkSize = 3overlap = 1return = [["a","b","c"],["c","d","e"],["e"]]

The window starts at indices 0, 2, and 4. Consecutive chunks overlap by one word.

Example 2

words = ["one","two","three","four"]chunkSize = 2overlap = 0return = [["one","two"],["three","four"]]

With zero overlap, each window begins immediately after the previous one.

Example 3

words = []chunkSize = 4overlap = 1return = []

There are no words, so no chunks are emitted.

Constraints

  • 0 <= words.length <= 10^5
  • 1 <= chunkSize <= 10^5
  • 0 <= overlap < chunkSize
  • Each word is a non-null string.

Similar questions at other companies

  • Sliding-Window Rate LimiterSnowflake · ONSITE INTERVIEWSnowflake interview questions
  • Incident MonitorStripe · OAStripe interview questions
  • Bad Request AlertsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Ring Buffer OperationsAmazon · ONSITE INTERVIEW
  • Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
  • Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
  • Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEWGoogle interview questions
  • Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
  • Policy Violation Set ChecksPinterest · ONSITE INTERVIEWPinterest interview questions
  • Retry with an Attempt LimitOracle · ONSITE INTERVIEWOracle interview questions
See SAP hiring insights
Saved
class Solution {
    public String[][] chunkText(String[] words, int chunkSize, int overlap) {
        // write your code here
    }
}
Checking account…