LearnViz CompSci
Home / Learn / Data Structures / Suffix Arrays

Advanced Topic 37 of 52 Data Structures

Suffix Arrays

Sorted array of all suffixes of a string.

How it works

A suffix array lists the starting indexes of all suffixes of a string in sorted order. It takes far less memory than a suffix tree, needs only n integers and supports substring search with binary search in O(m log n). Combined with an LCP array of longest common prefixes between neighbors, it answers many questions about repeated substrings. Construction can be done in O(n log n) or even O(n).

Step-by-step walkthrough

  1. Take all suffixes of the string banana.
  2. Sort them alphabetically: a, ana, anana, banana, na, nana.
  3. Store only their start positions: 5 3 1 0 4 2.
  4. Binary search finds any pattern in O(m log n).
  5. Neighbors in the sorted order share long prefixes.

Where it is used

Full-text search, genome alignment, data compression via the Burrows-Wheeler transform, finding the longest repeated or common substring and plagiarism detection use suffix arrays.

Common pitfall

Naive construction by sorting suffix strings costs O(n squared log n) in the worst case. Handling a sentinel character and comparing equal prefixes is error-prone. For multiple strings, concatenation with unique separators is needed. Remember the array holds positions, not substrings.

Where it fits in the course

Suffix Arrays is topic 37 of 52 in Data Structures, rated Advanced. It follows Sparse Tables. The next topic is Suffix Trees.

Practice and explore

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Suffix Arrays?
Sorted array of all suffixes of a string. It belongs to Data Structures and is an advanced-level topic.
Where do people use Suffix Arrays?
Full-text search, genome alignment, data compression via the Burrows-Wheeler transform, finding the longest repeated or common substring and plagiarism detection use suffix arrays.
What is the most common mistake with Suffix Arrays?
Naive construction by sorting suffix strings costs O(n squared log n) in the worst case.
What should I learn before this topic?
Start with Sparse Tables, the topic just before it in Data Structures.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Data Structures quiz.
Is Suffix Arrays hard to learn?
It is an Advanced topic. Expect to read the explanation more than once and to connect it with earlier topics. Working through a small example by hand helps most.
How long does it take to learn Suffix Arrays?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Suffix Arrays?
Sparse Tables, Suffix Trees, Treaps are closely connected. The full list is in the related topics section of this page.
What should I study after Suffix Arrays?
Continue with Suffix Trees, the next topic in Data Structures.

Related topics