1What is the main goal of a pattern matching algorithm?
Design of algorithms for pattern matching problems
Easy
A.To compress a text into a shorter form
B.To arrange the characters of a text alphabetically
C.To replace every character in a text
D.To find occurrences of a pattern in a text
Correct Answer: To find occurrences of a pattern in a text
Explanation:
Pattern matching algorithms locate one or more occurrences of a given pattern within a larger text.
Incorrect! Try again.
2In pattern matching, what does the term text usually mean?
Design of algorithms for pattern matching problems
Easy
A.The table used during preprocessing
B.The smaller sequence being located
C.The larger sequence being searched
D.The result returned after sorting
Correct Answer: The larger sequence being searched
Explanation:
The text is the larger sequence in which the algorithm searches for the pattern.
Incorrect! Try again.
3In pattern matching, what is a pattern?
Design of algorithms for pattern matching problems
Easy
A.The sequence being sorted
B.The sequence being divided
C.The sequence being searched for
D.The sequence being encrypted
Correct Answer: The sequence being searched for
Explanation:
A pattern is the smaller sequence that the algorithm attempts to locate in the text.
Incorrect! Try again.
4If a text has length and a pattern has length , how many possible starting alignments exist when ?
Design of algorithms for pattern matching problems
Easy
A.
B.
C.
D.
Correct Answer:
Explanation:
The pattern can begin at positions from through , giving possible alignments.
Incorrect! Try again.
5What does preprocessing a pattern mean?
Design of algorithms for pattern matching problems
Easy
A.Sorting all characters before searching
B.Deleting repeated characters before searching
C.Building information about the pattern before searching
D.Joining the pattern to another text
Correct Answer: Building information about the pattern before searching
Explanation:
Preprocessing builds data that can help the search algorithm avoid unnecessary comparisons.
Incorrect! Try again.
6How does the brute-force pattern matching algorithm search the text?
Brute-force algorithm
Easy
A.It divides the text into equal halves
B.It sorts the text before testing the pattern
C.It builds a tree from every text character
D.It tests the pattern at each possible alignment
Correct Answer: It tests the pattern at each possible alignment
Explanation:
Brute-force matching aligns the pattern at every possible starting position and compares characters.
Incorrect! Try again.
7After a mismatch, how far does the basic brute-force algorithm usually shift the pattern?
Brute-force algorithm
Easy
A.One position
B.The full pattern
C.Half the pattern
D.Two positions
Correct Answer: One position
Explanation:
The basic brute-force method moves the pattern one position to the right after a mismatch.
Incorrect! Try again.
8What is the worst-case time complexity of brute-force pattern matching for text length and pattern length ?
Brute-force algorithm
Easy
A.
B.
C.
D.
Correct Answer:
Explanation:
In the worst case, the algorithm performs up to comparisons at each of roughly alignments.
Incorrect! Try again.
9What preprocessing is required by the basic brute-force pattern matching algorithm?
Brute-force algorithm
Easy
A.A good-suffix table
B.An LPS table
C.A bad-character table
D.No special preprocessing
Correct Answer: No special preprocessing
Explanation:
The brute-force algorithm directly compares the pattern with the text and needs no preprocessing table.
Incorrect! Try again.
10When does brute-force pattern matching report a match at an alignment?
Brute-force algorithm
Easy
A.When the first pattern character matches
B.When half of the pattern characters match
C.When every pattern character matches
D.When the final pattern character matches
Correct Answer: When every pattern character matches
Explanation:
A complete match occurs only when all pattern characters equal the corresponding text characters.
Incorrect! Try again.
11What is the main advantage of the Knuth-Morris-Pratt algorithm?
Knuth-Morris-Pratt algorithm
Easy
A.It avoids rechecking previously matched text characters
B.It sorts the pattern before each comparison
C.It searches only the first half of the text
D.It converts the text into numeric values
Correct Answer: It avoids rechecking previously matched text characters
Explanation:
KMP uses information from earlier matches to continue efficiently after a mismatch.
Incorrect! Try again.
12Which table is commonly constructed during KMP preprocessing?
Knuth-Morris-Pratt algorithm
Easy
A.The adjacency table
B.The LPS table
C.The frequency table
D.The routing table
Correct Answer: The LPS table
Explanation:
KMP preprocesses the pattern to construct the LPS table.
Incorrect! Try again.
13What does LPS stand for in the KMP algorithm?
Knuth-Morris-Pratt algorithm
Easy
A.Largest Possible Substring within a Sequence
B.Longest Proper Prefix which is also a Suffix
C.Lowest Pattern Shift within a String
D.Last Pattern Symbol within a Search
Correct Answer: Longest Proper Prefix which is also a Suffix
Explanation:
LPS records the longest proper prefix of a pattern prefix that is also its suffix.
Incorrect! Try again.
14What is the time complexity of KMP for a text of length and a pattern of length ?
Knuth-Morris-Pratt algorithm
Easy
A.
B.
C.
D.
Correct Answer:
Explanation:
KMP preprocesses the pattern in time and searches the text in time.
Incorrect! Try again.
15Which part of the input is used to build the KMP LPS table?
Knuth-Morris-Pratt algorithm
Easy
A.The output
B.The pattern
C.The text
D.The alphabet
Correct Answer: The pattern
Explanation:
The LPS table is computed from prefix and suffix relationships within the pattern.
Incorrect! Try again.
16In which direction does Boyer-Moore typically compare pattern characters?
Boyer-Moore algorithm
Easy
A.From the center outward
B.From right to left
C.From left to right
D.From both ends alternately
Correct Answer: From right to left
Explanation:
Boyer-Moore normally begins comparisons at the rightmost character of the aligned pattern.
Incorrect! Try again.
17Which two heuristics are associated with the Boyer-Moore algorithm?
Boyer-Moore algorithm
Easy
A.Bad-character and good-suffix heuristics
B.Hashing and dynamic-programming heuristics
C.Prefix and binary-search heuristics
D.Greedy and divide-and-conquer heuristics
Correct Answer: Bad-character and good-suffix heuristics
Explanation:
Boyer-Moore uses bad-character and good-suffix information to choose efficient shifts.
Incorrect! Try again.
18What information does the bad-character heuristic use after a mismatch?
Boyer-Moore algorithm
Easy
A.The mismatching text character
B.The length of the text alone
C.The first character of the text
D.The total number of matches
Correct Answer: The mismatching text character
Explanation:
The bad-character heuristic uses the mismatching text character to determine a useful shift.
Incorrect! Try again.
19What information does the good-suffix heuristic use?
Boyer-Moore algorithm
Easy
A.A sorted copy of the entire pattern
B.A prefix that was never compared
C.A character outside the current text
D.A suffix that matched before the mismatch
Correct Answer: A suffix that matched before the mismatch
Explanation:
The good-suffix heuristic shifts the pattern based on a suffix that matched before a mismatch occurred.
Incorrect! Try again.
20Why can Boyer-Moore be faster than brute-force matching in practice?
Boyer-Moore algorithm
Easy
A.It always compares every text character twice
B.It can shift the pattern by multiple positions
C.It sorts each alignment before comparison
D.It removes duplicate characters from the text
Correct Answer: It can shift the pattern by multiple positions
Explanation:
Its heuristics often allow several text positions to be skipped after a mismatch.
Incorrect! Try again.
21A text has length and a pattern has length . How many valid alignments must an exact pattern-matching algorithm potentially examine?
Design of algorithms for pattern matching problems
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
The pattern can begin at positions through , giving valid alignments.
Incorrect! Try again.
22Which algorithm is most suitable when a text arrives as a stream and previously read text characters should not be revisited?
Design of algorithms for pattern matching problems
Medium
A.Brute-force matching
B.Basic Boyer-Moore matching
C.An algorithm that stores every alignment and compares all of them after the stream ends
D.Knuth-Morris-Pratt matching
Correct Answer: Knuth-Morris-Pratt matching
Explanation:
KMP processes the text from left to right without moving the text pointer backward, making it suitable for streaming input.
Incorrect! Try again.
23A case-insensitive matcher converts both the text and pattern to lowercase before searching. Why must the same normalization be applied to both?
Design of algorithms for pattern matching problems
Medium
A.To reduce the pattern length
B.To guarantee constant-time searching
C.To eliminate repeated substrings from the text
D.To preserve consistent equality comparisons
Correct Answer: To preserve consistent equality comparisons
Explanation:
Applying the same normalization ensures that corresponding text and pattern characters are compared under the same rules.
Incorrect! Try again.
24If every occurrence, including overlapping occurrences, must be reported, what should a correct pattern-matching algorithm do after finding a match?
Design of algorithms for pattern matching problems
Medium
A.Stop immediately after the match
B.Delete the matched text segment
C.Continue from a valid next alignment
D.Resume only after the end of the current matched substring, permanently excluding all overlaps
Correct Answer: Continue from a valid next alignment
Explanation:
The search must continue from an alignment that can still contain a match; otherwise, overlapping occurrences may be missed.
Incorrect! Try again.
25Using brute-force matching on the text AABAACAADAABAABA with pattern AABA, which zero-based starting positions are reported?
Brute-force algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
Direct comparison at every alignment finds AABA beginning at indices , , and .
Incorrect! Try again.
26A brute-force matcher reports all occurrences of pattern AAA in text AAAAAA. How many character comparisons does it perform if it compares the complete pattern at every valid alignment?
Brute-force algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
There are alignments, and all three characters match at each one, so the total is comparisons.
Incorrect! Try again.
27What is the worst-case running time of brute-force pattern matching for text length and pattern length ?
Brute-force algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
In the worst case, up to characters are compared at each of approximately alignments, giving time.
Incorrect! Try again.
28After a mismatch at an alignment, how does the standard brute-force algorithm choose the next alignment?
Brute-force algorithm
Medium
A.It moves the pattern past the entire previously compared region
B.It shifts according to the mismatched character
C.It shifts the pattern one position right
D.It uses the longest prefix table
Correct Answer: It shifts the pattern one position right
Explanation:
The standard brute-force method tests every possible alignment, so it advances the pattern by exactly one position after a mismatch.
Incorrect! Try again.
29What is the LPS array for the pattern ABABAC?
Knuth-Morris-Pratt algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
Each entry records the longest proper prefix that is also a suffix. For ABABA the length is , while ABABAC has no nonempty matching prefix and suffix.
Incorrect! Try again.
30During KMP matching, five pattern characters have matched, but the next comparison fails. If the LPS value for the fifth matched character is , what happens next?
Knuth-Morris-Pratt algorithm
Medium
A.Matching resumes at pattern index
B.The text pointer moves backward by
C.Matching resumes at pattern index
D.The current alignment is discarded and the pattern is always moved beyond all five matched characters
Correct Answer: Matching resumes at pattern index
Explanation:
KMP retains the known prefix-suffix match and sets the pattern index to the relevant LPS value, while the text pointer does not move backward.
Incorrect! Try again.
31What does an LPS value of at pattern index mean?
Knuth-Morris-Pratt algorithm
Medium
A.A length- proper prefix is also a suffix
B.The next match begins at text index
C.A length- substring appears only once in the complete text
D.Exactly mismatches occur before index
Correct Answer: A length- proper prefix is also a suffix
Explanation:
LPS stores the length of the longest proper prefix of the pattern prefix ending at that is also its suffix.
Incorrect! Try again.
32KMP searches for ABABCABAB in ABABDABACDABABCABAB. At which zero-based index does the pattern begin?
Knuth-Morris-Pratt algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
The substring from text index through is exactly ABABCABAB.
Incorrect! Try again.
33After KMP finds AAA in AAAAA, which pattern index should it use next to detect overlapping matches?
Knuth-Morris-Pratt algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
The final LPS value for AAA is , so KMP preserves the two-character overlap and can find matches at indices , , and .
Incorrect! Try again.
34Including preprocessing, what is the asymptotic time complexity of KMP for a text of length and a pattern of length ?
Knuth-Morris-Pratt algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
Constructing the LPS array takes time, and scanning the text takes time.
Incorrect! Try again.
35In which direction does the Boyer-Moore algorithm normally compare pattern characters at each alignment?
Boyer-Moore algorithm
Medium
A.From left to right
B.From right to left
C.From the middle outward
D.In alphabetic order
Correct Answer: From right to left
Explanation:
Boyer-Moore begins with the pattern's rightmost character, enabling its shift heuristics to skip multiple alignments.
Incorrect! Try again.
36For the pattern ALGORITHM, a mismatch occurs at pattern index against text character G. Using the bad-character rule and the rightmost G at pattern index , what shift is selected?
Boyer-Moore algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
The bad-character shift is .
Incorrect! Try again.
37A Boyer-Moore mismatch occurs at pattern index , and the mismatched text character does not occur in the pattern. What shift does the bad-character rule permit?
Boyer-Moore algorithm
Medium
A.
B. positions regardless of where the mismatch occurred
C.
D.
Correct Answer:
Explanation:
An absent character is treated as having last position , so the shift is .
Incorrect! Try again.
38What information is used by the good-suffix rule after a mismatch?
Boyer-Moore algorithm
Medium
A.The frequency of every pattern symbol across the entire input alphabet
B.The number of text characters already scanned
C.The first occurrence of the mismatched character
D.The suffix already matched at the alignment
Correct Answer: The suffix already matched at the alignment
Explanation:
The good-suffix rule shifts the pattern using another occurrence of the matched suffix or a prefix that matches part of that suffix.
Incorrect! Try again.
39If the bad-character rule suggests a shift of and the good-suffix rule suggests a shift of , what shift does standard Boyer-Moore apply?
Boyer-Moore algorithm
Medium
A.
B.
C.
D.
Correct Answer:
Explanation:
Boyer-Moore normally takes the larger valid shift supplied by its heuristics, so it shifts by .
Incorrect! Try again.
40Why can Boyer-Moore be especially effective on long patterns over a large alphabet?
Boyer-Moore algorithm
Medium
A.It always performs exactly comparisons
B.It avoids preprocessing the pattern
C.Its heuristics can skip several alignments
D.Its running time is constant whenever every pattern character is distinct and the text is stored contiguously
Correct Answer: Its heuristics can skip several alignments
Explanation:
Bad-character and good-suffix shifts often move the pattern several positions, reducing comparisons in typical inputs.
Incorrect! Try again.
41Let have length , let have length , and form , where occurs in neither string. Using zero-based indices, if the prefix function of satisfies , what is the starting index of the corresponding match in ?
Design of algorithms for pattern matching problems
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
The match ends at combined-string index . Since begins at index , its starting index is .
Incorrect! Try again.
42For two nonempty strings and of equal length , which test correctly determines in time whether is a cyclic rotation of ?
Design of algorithms for pattern matching problems
Hard
A.Search for in and accept a match starting in .
B.Search for in and accept only a match starting at .
C.Search for in and accept any complete match.
D.Search for in and accept a match ending before .
Correct Answer: Search for in and accept a match starting in .
Explanation:
Every rotation of appears in . Restricting the start to excludes the duplicate position .
Incorrect! Try again.
43A text arrives in chunks whose boundaries may split an occurrence of the pattern. Which design reports every match in worst-case linear total time without retaining the complete text?
Design of algorithms for pattern matching problems
Hard
A.Restart brute force at the beginning of every newly received text chunk.
B.Retain only the previous mismatch position and restart comparisons from that position.
C.Preserve KMP's matched-prefix length between chunks and preprocess the pattern once.
D.Run Boyer-Moore independently on each chunk and discard all boundary state.
Correct Answer: Preserve KMP's matched-prefix length between chunks and preprocess the pattern once.
Explanation:
KMP's state summarizes the relevant suffix of all processed text. Carrying across chunks detects boundary-spanning matches in total time.
Incorrect! Try again.
44Assume and arbitrary unstructured inputs. What is the optimal worst-case asymptotic time for reporting all occurrences of one length- pattern in a length- text?
Design of algorithms for pattern matching problems
Hard
A.; all text alignments can be inferred from the pattern alone.
B.; preprocessing is never required when the alphabet is finite.
C.; both inputs may require inspection, and KMP attains this bound.
D.; each possible alignment must be checked independently.
Correct Answer: ; both inputs may require inspection, and KMP attains this bound.
Explanation:
An adversary can force inspection of the pattern and text, giving . KMP matches this with time.
Incorrect! Try again.
45An occurrence is formally defined by for every integer . Under this definition, how should an algorithm handle an empty pattern when the text has length ?
Design of algorithms for pattern matching problems
Hard
A.Report the first boundaries from position through position .
B.Report exactly one occurrence at position and terminate the search.
C.Report no occurrences because an empty pattern contains no symbols.
D.Report all boundaries from position through position .
Correct Answer: Report all boundaries from position through position .
Explanation:
For , the permitted starts satisfy . An explicit empty-pattern case also prevents zero-length shifts or infinite loops.
Incorrect! Try again.
46A left-to-right brute-force matcher searches for , where . How many character comparisons are performed when every alignment is examined?
Brute-force algorithm
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
At each of the alignments, the first symbols match and the final comparison fails, producing exactly comparisons.
Incorrect! Try again.
47A left-to-right brute-force algorithm reports every match of in . Counting both successful and failed character tests, how many comparisons does it perform?
Brute-force algorithm
Hard
A. comparisons
B. comparisons
C. comparisons
D. comparisons
Correct Answer: comparisons
Explanation:
Alignments use four comparisons each, while alignments fail on their first comparisons. The total is .
Incorrect! Try again.
48Suppose text and pattern symbols are independent and uniformly distributed over an alphabet of size . What is the expected number of comparisons made by left-to-right brute force over all alignments?
Brute-force algorithm
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
The th comparison occurs only if the preceding symbols match, with probability . Summing this geometric series over all alignments gives the result.
Incorrect! Try again.
49For and , compare left-to-right and right-to-left brute-force matching over all alignments. By what asymptotic factor can right-to-left matching perform more comparisons?
Brute-force algorithm
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
Left-to-right matching fails on the first symbol, whereas right-to-left matching verifies symbols before failing. Their comparison counts differ by a factor of .
Incorrect! Try again.
50After brute force finds a complete match, it shifts the pattern by rather than by one. For which patterns is this optimization guaranteed to preserve every occurrence in every possible text?
Brute-force algorithm
Hard
A.Patterns whose first and final symbols are equal to each other.
B.Patterns having no nonempty proper prefix that is also a suffix.
C.Patterns having at least one symbol that occurs exactly once.
D.Patterns whose symbols are arranged in nondecreasing alphabetic order.
Correct Answer: Patterns having no nonempty proper prefix that is also a suffix.
Explanation:
Two occurrences can overlap only when the overlap is a proper border of the pattern. Thus a full-length shift is universally safe exactly for unbordered patterns.
Incorrect! Try again.
51Using the convention that is the length of the longest proper prefix that is also a suffix of , what is the prefix array for ?
Knuth-Morris-Pratt algorithm
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
At the third consecutive , fallback from border length leaves border , which extends to . The final then extends that border to .
Incorrect! Try again.
52For , KMP is in state , meaning that has matched. If the next text symbol is , what state does KMP enter after all necessary fallback operations?
Knuth-Morris-Pratt algorithm
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
The expected symbol mismatches, so KMP falls from to . The new symbol then matches , producing .
Incorrect! Try again.
53KMP searches for . Which result and post-match state are correct when overlapping occurrences must be reported?
Knuth-Morris-Pratt algorithm
Hard
A.Report starts and set after each complete match.
B.Report starts and set after each match.
C.Report starts and set after each complete match.
D.Report starts and set after each match.
Correct Answer: Report starts and set after each match.
Explanation:
The pattern's longest proper border is of length . Retaining that state after a match allows KMP to detect occurrences shifted by two positions.
Incorrect! Try again.
54Which amortization fact is sufficient to prove that KMP's text scan takes time even though one text symbol may trigger several fallback operations?
Knuth-Morris-Pratt algorithm
Hard
A. never decreases while the scan advances through the text.
B.Every text symbol participates in exactly one pattern comparison.
C.Total fallback decreases of are bounded by prior increases of .
D.The prefix values are strictly increasing throughout preprocessing.
Correct Answer: Total fallback decreases of are bounded by prior increases of .
Explanation:
The state cannot fall below zero, so its cumulative decrease is bounded by its cumulative increase. Since increases occur at most once per processed text symbol, the scan is linear.
Incorrect! Try again.
55For , the final prefix value is . Which sequence lists all nonempty proper border lengths from longest to shortest?
Knuth-Morris-Pratt algorithm
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
Repeatedly following gives , then , then , and finally .
Incorrect! Try again.
56In Boyer-Moore, let with zero-based indices. A mismatch occurs at pattern index , and the aligned text symbol is . What shift is proposed by the standard bad-character rule?
Boyer-Moore algorithm
Hard
A. positions
B. position
C. positions
D. positions
Correct Answer: positions
Explanation:
The rightmost in the pattern is at index . The bad-character shift is .
Incorrect! Try again.
57After Boyer-Moore matches the suffix but mismatches at , which action describes the strong good-suffix rule?
Boyer-Moore algorithm
Hard
A.Align the leftmost earlier occurrence of regardless of its preceding symbol; otherwise shift the pattern by exactly one position.
B.Align the rightmost earlier preceded by the same symbol as ; otherwise align the longest prefix of with a suffix of .
C.Ignore the matched suffix and shift according only to the last occurrence of the mismatching text symbol.
D.Align the rightmost earlier preceded by a symbol different from ; otherwise align the longest suffix of that is a prefix of .
Correct Answer: Align the rightmost earlier preceded by a symbol different from ; otherwise align the longest suffix of that is a prefix of .
Explanation:
The strong rule avoids an occurrence that would recreate the same mismatch. If no suitable occurrence exists, it preserves the largest suffix that can align with a pattern prefix.
Incorrect! Try again.
58After a complete match of , what shift does Boyer-Moore's good-suffix preprocessing prescribe to preserve overlapping matches?
Boyer-Moore algorithm
Hard
A. positions
B. position
C. positions
D. positions
Correct Answer: positions
Explanation:
The longest proper border is of length . A complete-match shift is , allowing the next overlapping occurrence.
Incorrect! Try again.
59Consider Boyer-Moore using only the standard bad-character rule, scanning right to left. For and , what is its worst-case comparison count?
Boyer-Moore algorithm
Hard
A.
B.
C.
D.
Correct Answer:
Explanation:
At every alignment, trailing symbols match before the first symbol fails. Since the mismatching occurs to the right in the pattern, the bad-character shift is only one.
Incorrect! Try again.
60A bad-character-only Boyer-Moore search has and . At every alignment, the final pattern symbol mismatches a text symbol absent from the pattern, so each shift is . Exactly how many character comparisons occur?
Boyer-Moore algorithm
Hard
A. comparisons
B. comparisons
C. comparisons
D. comparisons
Correct Answer: comparisons
Explanation:
Alignment starts are , the largest multiple of not exceeding . Their count is .
Incorrect! Try again.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. 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.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it.
What it pays for →