Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
Question 1 of 60 · The ubiquitous naïve pattern search problem · Easy
In the naïve pattern searching algorithm, the pattern is compared with the text starting at every possible position. What is the worst-case time complexity for a text of length and pattern of length ?
Not quite. Try another option.
Answer:
The naïve approach slides the pattern one position at a time and may compare up to characters at each of the roughly positions, giving in the worst case.
Question 2 of 60 · The ubiquitous naïve pattern search problem · Easy
How many starting positions in the text does the naïve algorithm check for a text of length and pattern of length ?
Not quite. Try another option.
Answer:
The pattern can start at index up to index , which gives possible alignments in the text.
Question 3 of 60 · KMP algorithm · Easy
What is the overall time complexity of the KMP (Knuth-Morris-Pratt) pattern matching algorithm for text length and pattern length ?
Not quite. Try another option.
Answer:
KMP preprocesses the pattern in to build the prefix table and then scans the text in , giving a total of .
Question 4 of 60 · KMP algorithm · Easy
What auxiliary structure does the KMP algorithm precompute from the pattern to avoid redundant comparisons?
Not quite. Try another option.
Answer: The LPS (longest prefix suffix) array
KMP builds the LPS array, which stores the length of the longest proper prefix that is also a suffix, allowing the search to skip already matched characters.
Question 5 of 60 · Rabin-Karp algorithm · Easy
Which technique is central to the Rabin-Karp pattern matching algorithm?
Not quite. Try another option.
Answer: Hashing (rolling hash)
Rabin-Karp compares hash values of the pattern and text windows using a rolling hash, only doing character checks when hashes match.
Question 6 of 60 · Rabin-Karp algorithm · Easy
In Rabin-Karp, when two substrings produce the same hash value but the actual characters differ, this event is called a:
Not quite. Try another option.
Answer: Spurious hit (collision)
A hash collision that is not an actual match is called a spurious hit, which is why Rabin-Karp verifies characters after a hash match.
Question 7 of 60 · Rabin-Karp algorithm · Easy
What is the average-case time complexity of the Rabin-Karp algorithm for text length and pattern length ?
Not quite. Try another option.
Answer:
With a good hash function producing few collisions, Rabin-Karp runs in on average, though the worst case can be .
Question 8 of 60 · Introduction to Suffix array · Easy
A suffix array of a string stores which of the following?
Not quite. Try another option.
Answer: The sorted order of all suffixes of the string
A suffix array is an array of integers giving the starting positions of all suffixes of a string in lexicographically sorted order.
Question 9 of 60 · Introduction to Suffix array · Easy
How many suffixes does a string of length have?
Not quite. Try another option.
Answer:
A string of length has exactly suffixes, one starting at each index from to .
Question 10 of 60 · Checking if two strings are rotations of each other · Easy
A common trick to check if string is a rotation of string is to verify that is a substring of:
Not quite. Try another option.
Answer: (A concatenated with itself)
If is a rotation of (and both have equal length), then will always appear as a substring of .
Question 11 of 60 · Checking if two strings are rotations of each other · Easy
For two strings to possibly be rotations of each other, what must first be true?
Not quite. Try another option.
Answer: They must have equal length
Rotations rearrange the same characters, so the strings must be of equal length; if lengths differ they cannot be rotations.
Question 12 of 60 · Largest connected component on a grid · Easy
Which traversal techniques are commonly used to find the largest connected component in a grid?
Not quite. Try another option.
Answer: DFS or BFS
Connected components on a grid are found by exploring neighboring cells using depth-first search (DFS) or breadth-first search (BFS).
Question 13 of 60 · Largest connected component on a grid · Easy
In a grid problem using 4-directional connectivity, a cell is connected to its neighbors in how many directions?
Not quite. Try another option.
Answer: 4 (up, down, left, right)
In 4-directional connectivity, each cell connects to its top, bottom, left, and right neighbors, giving 4 directions.
Question 14 of 60 · Check if a string is substring of another string · Easy
If pattern occurs somewhere inside text , then is called a __ of .
Not quite. Try another option.
Answer: substring
A contiguous sequence of characters appearing within another string is called a substring of that string.
Question 15 of 60 · Check if a string is substring of another string · Easy
Is the empty string "" considered a substring of every string?
Not quite. Try another option.
Answer: Yes, always
By convention, the empty string is a substring of every string, since it can be found at any position with zero-length match.
Question 16 of 60 · Longest prefix which is also a suffix · Easy
For the string "", what is the longest proper prefix that is also a suffix?
Not quite. Try another option.
Answer: ""
The proper prefixes are , , and the suffixes are , , . The longest common one is of length .
Question 17 of 60 · Longest prefix which is also a suffix · Easy
When computing the longest prefix which is also a suffix, why must it be a proper prefix/suffix?
Not quite. Try another option.
Answer: To exclude the whole string itself
A proper prefix/suffix excludes the entire string, otherwise the whole string would trivially be the answer for every string.
Question 18 of 60 · Problems based on naive pattern search · Easy
Using naïve search, how many times does the pattern "" occur in the text ""?
Not quite. Try another option.
Answer: 3
The pattern "" matches at positions , , and in "", giving occurrences (overlaps counted).
Question 19 of 60 · Lexicographical sorting of strings with practice problems based on this concept · Easy
Which of the following lists is in correct lexicographical (dictionary) order?
Not quite. Try another option.
Answer: ["apple", "apply", "banana"]
Lexicographical order compares character by character: "apple" < "apply" (since 'e' < 'y') and both come before "banana" (since 'a' < 'b').
Question 20 of 60 · Splitting a string into substrings with suitable practice problems · Easy
Splitting the string "" on the delimiter "" produces how many substrings?
Not quite. Try another option.
Answer: 3
Splitting "" on the comma delimiter yields the substrings "", "", and "", which is parts.
Question 21 of 60 · The ubiquitous naïve pattern search problem · Medium
For a text of length and a pattern of length , what is the worst-case time complexity of the naïve pattern search algorithm?
Not quite. Try another option.
Answer:
In the worst case (e.g., text AAAA...A and pattern AAA...B), each of the shifts requires up to comparisons, giving .
Question 22 of 60 · The ubiquitous naïve pattern search problem · Medium
Which text-pattern pair triggers the worst-case behavior of naïve pattern matching?
Not quite. Try another option.
Answer: Text AAAAAA, pattern AAB
Repeated characters in the text with a mismatch at the last pattern position force nearly comparisons at every shift, producing the worst case.
Question 23 of 60 · KMP algorithm · Medium
In the KMP algorithm, what does the LPS (Longest Prefix Suffix) array store for each index of the pattern?
Not quite. Try another option.
Answer: Length of the longest proper prefix of pat[0..i] that is also a suffix
The LPS array records, for each prefix, the length of the longest proper prefix that is also a suffix, letting KMP skip redundant comparisons after a mismatch.
Question 24 of 60 · KMP algorithm · Medium
What is the overall time complexity of the KMP algorithm for text length and pattern length ?
Not quite. Try another option.
Answer:
Building the LPS array takes and the search phase takes since neither pointer moves backward, giving a total of .
Question 25 of 60 · KMP algorithm · Medium
For the pattern ABABAA, what is the correct LPS array?
Not quite. Try another option.
Answer:
Computing prefix-suffix lengths: A→0, AB→0, ABA→1, ABAB→2, ABABA→3, ABABAA→1 (since the final A matches only the prefix of length 1).
Question 26 of 60 · Rabin-Karp algorithm · Medium
In the Rabin-Karp algorithm, why is a hash comparison alone insufficient to confirm a pattern match?
Not quite. Try another option.
Answer: Different substrings may share the same hash (collision)
Hash collisions mean two different substrings can produce equal hashes, so a character-by-character verification is required whenever hashes match.
Question 27 of 60 · Rabin-Karp algorithm · Medium
What is the key advantage of using a rolling hash in Rabin-Karp?
Not quite. Try another option.
Answer: The hash of the next window is computed in from the previous
A rolling hash updates by removing the leading character's contribution and adding the new trailing character, allowing constant-time window updates.
Question 28 of 60 · Rabin-Karp algorithm · Medium
What is the average-case time complexity of Rabin-Karp for text length and pattern length with a good hash?
Not quite. Try another option.
Answer:
With few collisions, hashing gives on average, though the worst case degrades to when many spurious hits require verification.
Question 29 of 60 · Introduction to Suffix array · Medium
What does a suffix array of a string store?
Not quite. Try another option.
Answer: Starting indices of all suffixes sorted in lexicographical order
A suffix array is an array of the starting positions of every suffix of the string, arranged so the suffixes appear in sorted (lexicographical) order.
Question 30 of 60 · Introduction to Suffix array · Medium
For the string banana, which suffix comes first in its suffix array?
Not quite. Try another option.
Answer: a
Sorting all suffixes lexicographically, a (starting at index 5) is the smallest, so it appears first in the suffix array.
Question 31 of 60 · Checking if two strings are rotations of each other · Medium
A common trick checks if string B is a rotation of A by testing whether B is a substring of which string?
Not quite. Try another option.
Answer: A + A
All rotations of A appear as contiguous substrings of A + A, so B is a rotation of A iff A and B have equal length and B is a substring of A + A.
Question 32 of 60 · Checking if two strings are rotations of each other · Medium
Which pair of strings are rotations of each other?
Not quite. Try another option.
Answer: abcd and cdab
cdab appears as a substring of abcdabcd, confirming it is a rotation. The others either reorder characters or differ in length.
Question 33 of 60 · Largest connected component on a grid · Medium
Which technique is most commonly used to find the largest connected component of cells in a grid?
Not quite. Try another option.
Answer: Depth-First Search / BFS flood fill
A flood-fill traversal (DFS or BFS) from each unvisited target cell explores its whole component; tracking the largest size found solves the problem.
Question 34 of 60 · Largest connected component on a grid · Medium
For a grid with rows and columns using 4-directional connectivity, what is the time complexity of a flood-fill solution?
Not quite. Try another option.
Answer:
Each cell is visited once and marked, so the total work is proportional to the number of cells, .
Question 35 of 60 · Check if a string is substring of another string · Medium
Using naïve substring checking, how many starting positions must be examined when searching pattern of length in text of length ?
Not quite. Try another option.
Answer:
The pattern can align at indices through , giving exactly candidate starting positions.
Question 36 of 60 · Check if a string is substring of another string · Medium
Which statement about the empty string as a pattern is correct?
Not quite. Try another option.
Answer: The empty string is a substring of every string
By definition, the empty string matches at every position (including position 0) of any string, so it is a substring of all strings.
Question 37 of 60 · Longest prefix which is also a suffix · Medium
For the string aabaaab, what is the length of the longest proper prefix that is also a suffix?
Not quite. Try another option.
Answer:
The prefix aa (length 2) matches the suffix ab? No—checking carefully, the longest proper prefix equal to a suffix is aa... the string ends in aab, whose matching prefix is aa of length 2.
Question 38 of 60 · Longest prefix which is also a suffix · Medium
The 'longest prefix which is also a suffix' concept is directly used to build which structure?
Not quite. Try another option.
Answer: The LPS/failure array in KMP
KMP precomputes the longest proper prefix-suffix length for each prefix, forming the failure (LPS) array that guides efficient shifts.
Question 39 of 60 · Problems based on naive pattern search · Medium
To count all overlapping occurrences of a pattern in a text using naïve search, what should you do after finding a match at index ?
Not quite. Try another option.
Answer: Continue searching from index
To capture overlapping matches (e.g., aa in aaa), the search must resume from the next index rather than skipping past the whole matched pattern.
Question 40 of 60 · Lexicographical sorting of strings with practice problems based on this concept · Medium
When sorting the strings ["apple", "app", "apply"] lexicographically, what is the correct order?
Not quite. Try another option.
Answer: app, apple, apply
A shorter string that is a prefix of another comes first, so app precedes apple; then apple precedes apply because l < l... comparing at index 4, e < y, so apple comes before apply.
Question 41 of 60 · The ubiquitous naïve pattern search problem · Hard
Consider naïve pattern searching of a pattern of length in a text of length . What text/pattern combination triggers the algorithm's absolute worst-case number of character comparisons?
Not quite. Try another option.
Answer: $T = $ aaaa...a and $P = $ aaa...ab
When is all as and is aaa...ab, at every one of the alignments the algorithm matches characters before failing on the last, giving comparisons — the true worst case. Mismatch-early cases finish fast.
Question 42 of 60 · KMP algorithm · Hard
For the pattern $P = $ aabaabaaa, what is the KMP failure/LPS array (longest proper prefix that is also suffix for each prefix)?
Not quite. Try another option.
Answer:
Computing LPS for aabaabaaa: indices give . At the last a, the LPS drops from to because after aabaab the longest border re-extends only to length (aa).
Question 43 of 60 · KMP algorithm · Hard
During KMP matching, when a mismatch occurs at pattern index (with ), what is the correct next action?
Not quite. Try another option.
Answer: Set without advancing the text index
On mismatch at , KMP shifts the pattern using the border information: becomes while the text pointer stays put, so already-matched prefix knowledge is reused and no backtracking occurs in .
Question 44 of 60 · Rabin-Karp algorithm · Hard
In Rabin-Karp with a rolling hash, why can the worst-case time complexity degrade to despite the rolling hash being per shift?
Not quite. Try another option.
Answer: Spurious hash collisions force character-by-character verification at every window
If the hash function collides at every window (e.g., an adversarial input or tiny modulus), each of the windows requires an explicit comparison to rule out false positives, yielding overall.
Question 45 of 60 · Rabin-Karp algorithm · Hard
For rolling hash , the update when sliding one position removes the leading char and adds trailing . Which formula is correct?
Not quite. Try another option.
Answer:
First subtract the high-order contribution , multiply the remainder by to shift all positions up, then add the new low-order character . All under .
Question 46 of 60 · Introduction to Suffix array · Hard
For the string $S = $ banana, what is the suffix array (starting indices, 0-based, sorted lexicographically)?
Not quite. Try another option.
Answer:
Suffixes: a(5), ana(3), anana(1), banana(0), na(4), nana(2). Sorting: a < ana < anana < banana < na < nana, giving indices .
Question 47 of 60 · Introduction to Suffix array · Hard
Using the fastest practical construction (prefix-doubling with radix sort), what is the time complexity to build a suffix array for a string of length ?
Not quite. Try another option.
Answer:
Prefix doubling performs rounds, each ranking suffixes by their first characters using radix sort in , giving . (Naïve comparison sort gives ; linear-time algorithms like DC3/SA-IS exist but are more complex.)
Question 48 of 60 · Checking if two strings are rotations of each other · Hard
The standard trick to test if is a rotation of (equal lengths ) is to check whether is a substring of . Using KMP, what is the time and space complexity?
Not quite. Try another option.
Answer: time, space
Concatenating is length; running KMP to find within it is time. The LPS array and the concatenated string require space.
Question 49 of 60 · Checking if two strings are rotations of each other · Hard
Given $A = $ abcde, which of the following is NOT a rotation of ?
Not quite. Try another option.
Answer: abced
Rotations of abcde are the cyclic shifts appearing as substrings of abcdeabcde: bcdea, cdeab, deabc, eabcd. The string abced swaps d and e internally — it is a rearrangement, not a rotation.
Question 50 of 60 · Largest connected component on a grid · Hard
For finding the largest connected component of 1s in an binary grid using DFS/BFS with 8-directional connectivity, what is the correct time complexity?
Not quite. Try another option.
Answer:
Each cell is visited once and, on visiting, examines a constant number (8) of neighbors. Total work is , independent of connectivity direction count being constant.
Question 51 of 60 · Largest connected component on a grid · Hard
When using a Union-Find (DSU) approach to compute the largest connected component in a grid, why is it sufficient to union each cell only with its right and bottom neighbors (for 4-connectivity)?
Not quite. Try another option.
Answer: Left and top unions are implied by symmetry when every cell is processed
Union is symmetric: unioning cell with its right neighbor also connects that neighbor's left edge. Iterating over all cells and unioning right + bottom covers every adjacency exactly once, avoiding redundant work.
Question 52 of 60 · Check if a string is substring of another string · Hard
You must check if pattern ( chars) is a substring of text ( chars) with guaranteed worst-case linear time AND no risk of false positives from hashing. Which algorithm best fits?
Not quite. Try another option.
Answer: KMP algorithm
KMP guarantees worst case and is deterministic (no hashing false positives). Rabin-Karp risks collisions/false positives without verification and can degrade to ; naïve is .
Question 53 of 60 · Longest prefix which is also a suffix · Hard
For $S = $ abababab, what is the length of the longest proper prefix that is also a suffix?
Not quite. Try another option.
Answer:
The proper prefix ababab (length 6) equals the suffix ababab. Length 7 would be improper (must not equal the whole string, but here abababa suffix anyway). Thus the LPS value of the full string is .
Question 54 of 60 · Longest prefix which is also a suffix · Hard
The smallest period of a string of length equals , where is the longest proper prefix-suffix length. For $S = $ aabaaab (length 7) with , what is the smallest period, and does the string have a period dividing ?
Not quite. Try another option.
Answer: Period ; it does not divide , so is not fully periodic
Smallest period . Since does not divide , cannot be written as whole repetitions of a length-5 block, so it is not fully periodic.
Question 55 of 60 · Problems based on naive pattern search · Hard
You want to count overlapping occurrences of pattern in text using naïve search. After a match at position , how should the search index advance to correctly count overlaps?
Not quite. Try another option.
Answer: Advance by position
To count overlapping occurrences with naïve search, after finding a match you move the window by a single position so overlapping alignments (e.g., aa in aaa) are all counted. Advancing by would miss overlaps.
Question 56 of 60 · Problems based on naive pattern search · Hard
Given text aaaaa and pattern aa, how many overlapping occurrences exist, and how many non-overlapping (greedy left-to-right) occurrences exist?
Not quite. Try another option.
Answer: Overlapping , non-overlapping
Overlapping matches start at indices → 4 occurrences. Greedy non-overlapping consumes aa at 0–1 and 2–3, leaving one leftover a → 2 occurrences.
Question 57 of 60 · Lexicographical sorting of strings with practice problems based on this concept · Hard
To arrange a list of numbers as strings so their concatenation forms the largest possible number, which comparator should sort two strings and ?
Not quite. Try another option.
Answer: Place before if (string concatenation compared lexicographically)
The correct order compares the two possible concatenations: if then should come first. E.g., for 9 and 34, 934 > 349, so 9 precedes 34. Plain numeric or length sorting fails cases like 3 vs 30.
Question 58 of 60 · Lexicographical sorting of strings with practice problems based on this concept · Hard
When sorting the strings ["3", "30", "34", "5", "9"] to form the largest number, what is the resulting concatenation?
Not quite. Try another option.
Answer: 9534330
Using the vs comparator, the order becomes 9,5,34,3,30 → concatenation 9534330. Note 3 precedes 30 because 330 > 303.
Question 59 of 60 · Splitting a string into substrings with suitable practice problems · Hard
Consider partitioning a string of length into contiguous substrings such that each substring is a palindrome, and you want the minimum number of cuts. What is the time complexity of the standard DP solution?
Not quite. Try another option.
Answer:
Precompute an palindrome table in , then run a 1-D DP over cut positions where each state checks splits — also . Total . The naïve exponential enumeration is .
Question 60 of 60 · Splitting a string into substrings with suitable practice problems · Hard
You must count the number of ways to split a binary string into non-empty substrings such that each part, read as binary, is a power of . Which algorithmic technique most directly gives an efficient solution?
Not quite. Try another option.
Answer: Dynamic programming over prefix boundaries with a validity check per substring
Define = number of valid partitions of the prefix of length ; transition by trying every previous cut and adding if substring is a valid power-of-5 binary. This is DP. Greedy fails because valid splits can require non-longest choices.
That's unit 5
Answer a few questions and your score shows up here.
Nearly through. Did unit 5 of CSE329 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 5 of CSE329 save you a night before the exam?
Then you can help keep it free for the next batch. Ads cover part of the server bill;
the rest comes out of a student's own pocket: the domain, the storage, and keeping
the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason.
to try again.