Unit 5: Naive Pattern Search - Practice Quiz

CSE329 — Prelude To Competitive Coding 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 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 ?

The ubiquitous naïve pattern search problem Easy
A.
B.
C.
D.

2 How many starting positions in the text does the naïve algorithm check for a text of length and pattern of length ?

The ubiquitous naïve pattern search problem Easy
A.
B.
C.
D.

3 What is the overall time complexity of the KMP (Knuth-Morris-Pratt) pattern matching algorithm for text length and pattern length ?

KMP algorithm Easy
A.
B.
C.
D.

4 What auxiliary structure does the KMP algorithm precompute from the pattern to avoid redundant comparisons?

KMP algorithm Easy
A. A frequency count of characters
B. A hash table of substrings
C. The LPS (longest prefix suffix) array
D. A suffix tree

5 Which technique is central to the Rabin-Karp pattern matching algorithm?

Rabin-Karp algorithm Easy
A. Hashing (rolling hash)
B. Dynamic programming
C. Sorting
D. Binary search

6 In Rabin-Karp, when two substrings produce the same hash value but the actual characters differ, this event is called a:

Rabin-Karp algorithm Easy
A. Perfect match
B. Cache miss
C. Overflow
D. Spurious hit (collision)

7 What is the average-case time complexity of the Rabin-Karp algorithm for text length and pattern length ?

Rabin-Karp algorithm Easy
A.
B.
C.
D.

8 A suffix array of a string stores which of the following?

Introduction to Suffix array Easy
A. The reversed string
B. All prefixes of the string
C. The frequency of each character
D. The sorted order of all suffixes of the string

9 How many suffixes does a string of length have?

Introduction to Suffix array Easy
A.
B.
C.
D.

10 A common trick to check if string is a rotation of string is to verify that is a substring of:

Checking if two strings are rotations of each other Easy
A. (A concatenated with itself)
B.
C.
D. the reverse of

11 For two strings to possibly be rotations of each other, what must first be true?

Checking if two strings are rotations of each other Easy
A. They must start with the same character
B. They must contain distinct characters
C. One must be a prefix of the other
D. They must have equal length

12 Which traversal techniques are commonly used to find the largest connected component in a grid?

Largest connected component on a grid Easy
A. DFS or BFS
B. Binary search
C. Merge sort
D. Hashing

13 In a grid problem using 4-directional connectivity, a cell is connected to its neighbors in how many directions?

Largest connected component on a grid Easy
A. 4 (up, down, left, right)
B. 2 (left, right)
C. 8 (including diagonals)
D. 1

14 If pattern occurs somewhere inside text , then is called a __ of .

Check if a string is substring of another string Easy
A. substring
B. palindrome
C. superstring
D. rotation

15 Is the empty string "" considered a substring of every string?

Check if a string is substring of another string Easy
A. No, never
B. Yes, always
C. Only for strings of even length
D. Only for palindromes

16 For the string "", what is the longest proper prefix that is also a suffix?

Longest prefix which is also a suffix Easy
A. ""
B. ""
C. ""
D. ""

17 When computing the longest prefix which is also a suffix, why must it be a proper prefix/suffix?

Longest prefix which is also a suffix Easy
A. To include only vowels
B. To ensure it is a palindrome
C. To make it case-insensitive
D. To exclude the whole string itself

18 Using naïve search, how many times does the pattern "" occur in the text ""?

Problems based on naive pattern search Easy
A. 3
B. 2
C. 1
D. 4

19 Which of the following lists is in correct lexicographical (dictionary) order?

Lexicographical sorting of strings with practice problems based on this concept Easy
A. ["apple", "apply", "banana"]
B. ["banana", "apple", "apply"]
C. ["apply", "apple", "banana"]
D. ["banana", "apply", "apple"]

20 Splitting the string "" on the delimiter "" produces how many substrings?

Splitting a string into substrings with suitable practice problems Easy
A. 3
B. 2
C. 1
D. 4

21 For a text of length and a pattern of length , what is the worst-case time complexity of the naïve pattern search algorithm?

The ubiquitous naïve pattern search problem Medium
A.
B.
C.
D.

22 Which text-pattern pair triggers the worst-case behavior of naïve pattern matching?

The ubiquitous naïve pattern search problem Medium
A. Text AAAAAA, pattern AAB
B. Text ABCDEF, pattern DEF
C. Text ABABAB, pattern XY
D. Text ABCDEF, pattern XYZ

23 In the KMP algorithm, what does the LPS (Longest Prefix Suffix) array store for each index of the pattern?

KMP algorithm Medium
A. Number of occurrences of pat[i] in the text
B. Index of the last mismatch encountered
C. Length of the longest proper prefix of pat[0..i] that is also a suffix
D. Length of the longest repeating substring ending at

24 What is the overall time complexity of the KMP algorithm for text length and pattern length ?

KMP algorithm Medium
A.
B.
C.
D.

25 For the pattern ABABAA, what is the correct LPS array?

KMP algorithm Medium
A.
B.
C.
D.

26 In the Rabin-Karp algorithm, why is a hash comparison alone insufficient to confirm a pattern match?

Rabin-Karp algorithm Medium
A. Hashes cannot be computed for strings
B. The rolling hash ignores character order entirely
C. Hash values are always unique per substring
D. Different substrings may share the same hash (collision)

27 What is the key advantage of using a rolling hash in Rabin-Karp?

Rabin-Karp algorithm Medium
A. It sorts the text before searching
B. It removes the need to store the pattern
C. It eliminates all hash collisions
D. The hash of the next window is computed in from the previous

28 What is the average-case time complexity of Rabin-Karp for text length and pattern length with a good hash?

Rabin-Karp algorithm Medium
A.
B.
C.
D.

29 What does a suffix array of a string store?

Introduction to Suffix array Medium
A. All suffixes stored in a tree structure
B. Hash values of every suffix of
C. Starting indices of all suffixes sorted in lexicographical order
D. Lengths of all suffixes in decreasing order

30 For the string banana, which suffix comes first in its suffix array?

Introduction to Suffix array Medium
A. ana
B. a
C. banana
D. nana

31 A common trick checks if string B is a rotation of A by testing whether B is a substring of which string?

Checking if two strings are rotations of each other Medium
A. reverse(A)
B. B + B
C. A + A
D. A + B

32 Which pair of strings are rotations of each other?

Checking if two strings are rotations of each other Medium
A. abcd and abdc
B. abcd and dcba
C. abcd and abccd
D. abcd and cdab

33 Which technique is most commonly used to find the largest connected component of cells in a grid?

Largest connected component on a grid Medium
A. Binary search on the grid rows
B. Dynamic programming on grid diagonals
C. Depth-First Search / BFS flood fill
D. Merge sort of the cell values

34 For a grid with rows and columns using 4-directional connectivity, what is the time complexity of a flood-fill solution?

Largest connected component on a grid Medium
A.
B.
C.
D.

35 Using naïve substring checking, how many starting positions must be examined when searching pattern of length in text of length ?

Check if a string is substring of another string Medium
A.
B.
C.
D.

36 Which statement about the empty string as a pattern is correct?

Check if a string is substring of another string Medium
A. The empty string is never a substring
B. The empty string equals every string
C. The empty string is a substring only of the empty string
D. The empty string is a substring of every string

37 For the string aabaaab, what is the length of the longest proper prefix that is also a suffix?

Longest prefix which is also a suffix Medium
A.
B.
C.
D.

38 The 'longest prefix which is also a suffix' concept is directly used to build which structure?

Longest prefix which is also a suffix Medium
A. The adjacency list of a grid
B. The suffix array via sorting
C. The LPS/failure array in KMP
D. The rolling hash in Rabin-Karp

39 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 ?

Problems based on naive pattern search Medium
A. Stop the search immediately
B. Continue searching from index
C. Restart the search from index
D. Continue searching from index

40 When sorting the strings ["apple", "app", "apply"] lexicographically, what is the correct order?

Lexicographical sorting of strings with practice problems based on this concept Medium
A. app, apple, apply
B. apple, apply, app
C. apply, apple, app
D. app, apply, apple

41 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?

The ubiquitous naïve pattern search problem Hard
A. All characters of are distinct
B. and share no common characters
C. occurs exactly once at the start of
D. $T = $ aaaa...a and $P = $ aaa...ab

42 For the pattern $P = $ aabaabaaa, what is the KMP failure/LPS array (longest proper prefix that is also suffix for each prefix)?

KMP algorithm Hard
A.
B.
C.
D.

43 During KMP matching, when a mismatch occurs at pattern index (with ), what is the correct next action?

KMP algorithm Hard
A. Set and advance the text index
B. Reset and advance the text index
C. Set and keep the text index
D. Set without advancing the text index

44 In Rabin-Karp with a rolling hash, why can the worst-case time complexity degrade to despite the rolling hash being per shift?

Rabin-Karp algorithm Hard
A. The modular arithmetic itself takes per operation
B. Spurious hash collisions force character-by-character verification at every window
C. Recomputing the hash from scratch each window costs
D. Sorting the hash values dominates the runtime

45 For rolling hash , the update when sliding one position removes the leading char and adds trailing . Which formula is correct?

Rabin-Karp algorithm Hard
A.
B.
C.
D.

46 For the string $S = $ banana, what is the suffix array (starting indices, 0-based, sorted lexicographically)?

Introduction to Suffix array Hard
A.
B.
C.
D.

47 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 ?

Introduction to Suffix array Hard
A. with no improvement possible
B.
C. always
D.

48 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?

Checking if two strings are rotations of each other Hard
A. time, space
B. time, space
C. time, space
D. time, space

49 Given $A = $ abcde, which of the following is NOT a rotation of ?

Checking if two strings are rotations of each other Hard
A. abced
B. bcdea
C. cdeab
D. eabcd

50 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?

Largest connected component on a grid Hard
A.
B.
C.
D.

51 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)?

Largest connected component on a grid Hard
A. It reduces connectivity to a spanning tree only
B. Left and top unions are implied by symmetry when every cell is processed
C. Right and bottom neighbors are the only true neighbors in a grid
D. DSU cannot process left/top edges correctly

52 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?

Check if a string is substring of another string Hard
A. Naïve search
B. KMP algorithm
C. Random-modulus rolling hash without verification
D. Rabin-Karp with a single small prime modulus

53 For $S = $ abababab, what is the length of the longest proper prefix that is also a suffix?

Longest prefix which is also a suffix Hard
A.
B.
C.
D.

54 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 ?

Longest prefix which is also a suffix Hard
A. Period ; it divides evenly, so is fully periodic
B. Period ; it does not divide , so is not fully periodic
C. Period ; it does not divide
D. Period ; the string is aperiodic

55 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?

Problems based on naive pattern search Hard
A. Advance by (pattern length)
B. Advance by
C. Advance by position
D. Advance by

56 Given text aaaaa and pattern aa, how many overlapping occurrences exist, and how many non-overlapping (greedy left-to-right) occurrences exist?

Problems based on naive pattern search Hard
A. Overlapping , non-overlapping
B. Overlapping , non-overlapping
C. Overlapping , non-overlapping
D. Overlapping , non-overlapping

57 To arrange a list of numbers as strings so their concatenation forms the largest possible number, which comparator should sort two strings and ?

Lexicographical sorting of strings with practice problems based on this concept Hard
A. Place before if (string concatenation compared lexicographically)
B. Sort by descending string length, then lexicographically
C. Sort by descending numeric value of each string
D. Place before if lexicographically

58 When sorting the strings ["3", "30", "34", "5", "9"] to form the largest number, what is the resulting concatenation?

Lexicographical sorting of strings with practice problems based on this concept Hard
A. 9345330
B. 9553430
C. 9534330
D. 9534303

59 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?

Splitting a string into substrings with suitable practice problems Hard
A.
B.
C.
D.

60 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?

Splitting a string into substrings with suitable practice problems Hard
A. Rabin-Karp rolling hash of the whole string
B. Union-Find over character positions
C. Dynamic programming over prefix boundaries with a validity check per substring
D. Greedy leftmost-longest matching