Start by clarifying the requirements and edge cases, then outline a step-by-step algorithm: iterate through sentences, tokenize each sentence by splitting on whitespace, normalize tokens by lowercasing and stripping punctuation, and insert sentence indices into a hash map where each word maps to a list. After processing all sentences, sort each list (or maintain sorted order during insertion) and analyze time complexity as O(N + W log W) where N is total tokens and W is total unique word occurrences.
Pro tip: Mention that you can avoid sorting by processing sentences in order and appending indices, which naturally keeps lists sorted, reducing complexity to O(N). This shows attention to efficiency and practical optimization.
Ask about punctuation handling (e.g., hyphens, apostrophes), case sensitivity, and whether to include empty tokens. Confirm that sentence indices are 0-based or 1-based.
Use a hash map (dictionary) where keys are normalized words and values are lists of sentence indices. Consider using a set to avoid duplicates if a word appears multiple times in the same sentence, but clarify if duplicates are needed.
For each sentence, split on whitespace, convert to lowercase, and strip punctuation using a regex or string translation. Skip empty tokens.
Iterate through sentences in order, and for each token, append the sentence index to the word's list. Since sentences are processed in order, lists remain sorted; otherwise, sort each list at the end.
Time: O(N + U log U) if sorting, or O(N) if appending in order, where N is total tokens and U is total unique word occurrences. Space: O(N) for the index.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.