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.
You're offline
Unit 8 MCQsPattern Matching
ECAP538 ·
60 questions. Pick an option to check it.
Question 1 of 60 · Design of algorithms for pattern matching problems · Easy
What is the main goal of a pattern matching algorithm?
Not quite. Try another option.
Answer: To find occurrences of a pattern in a text
Pattern matching algorithms locate one or more occurrences of a given pattern within a larger text.
Question 2 of 60 · Design of algorithms for pattern matching problems · Easy
In pattern matching, what does the term text usually mean?
Not quite. Try another option.
Answer: The larger sequence being searched
The text is the larger sequence in which the algorithm searches for the pattern.
Question 3 of 60 · Design of algorithms for pattern matching problems · Easy
In pattern matching, what is a pattern?
Not quite. Try another option.
Answer: The sequence being searched for
A pattern is the smaller sequence that the algorithm attempts to locate in the text.
Question 4 of 60 · Design of algorithms for pattern matching problems · Easy
If a text has length and a pattern has length , how many possible starting alignments exist when ?
Not quite. Try another option.
Answer:
The pattern can begin at positions from through , giving possible alignments.
Question 5 of 60 · Design of algorithms for pattern matching problems · Easy
What does preprocessing a pattern mean?
Not quite. Try another option.
Answer: Building information about the pattern before searching
Preprocessing builds data that can help the search algorithm avoid unnecessary comparisons.
Question 6 of 60 · Brute-force algorithm · Easy
How does the brute-force pattern matching algorithm search the text?
Not quite. Try another option.
Answer: It tests the pattern at each possible alignment
Brute-force matching aligns the pattern at every possible starting position and compares characters.
Question 7 of 60 · Brute-force algorithm · Easy
After a mismatch, how far does the basic brute-force algorithm usually shift the pattern?
Not quite. Try another option.
Answer: One position
The basic brute-force method moves the pattern one position to the right after a mismatch.
Question 8 of 60 · Brute-force algorithm · Easy
What is the worst-case time complexity of brute-force pattern matching for text length and pattern length ?
Not quite. Try another option.
Answer:
In the worst case, the algorithm performs up to comparisons at each of roughly alignments.
Question 9 of 60 · Brute-force algorithm · Easy
What preprocessing is required by the basic brute-force pattern matching algorithm?
Not quite. Try another option.
Answer: No special preprocessing
The brute-force algorithm directly compares the pattern with the text and needs no preprocessing table.
Question 10 of 60 · Brute-force algorithm · Easy
When does brute-force pattern matching report a match at an alignment?
Not quite. Try another option.
Answer: When every pattern character matches
A complete match occurs only when all pattern characters equal the corresponding text characters.
Question 11 of 60 · Knuth-Morris-Pratt algorithm · Easy
What is the main advantage of the Knuth-Morris-Pratt algorithm?
Not quite. Try another option.
Answer: It avoids rechecking previously matched text characters
KMP uses information from earlier matches to continue efficiently after a mismatch.
Question 12 of 60 · Knuth-Morris-Pratt algorithm · Easy
Which table is commonly constructed during KMP preprocessing?
Not quite. Try another option.
Answer: The LPS table
KMP preprocesses the pattern to construct the LPS table.
Question 13 of 60 · Knuth-Morris-Pratt algorithm · Easy
What does LPS stand for in the KMP algorithm?
Not quite. Try another option.
Answer: Longest Proper Prefix which is also a Suffix
LPS records the longest proper prefix of a pattern prefix that is also its suffix.
Question 14 of 60 · Knuth-Morris-Pratt algorithm · Easy
What is the time complexity of KMP for a text of length and a pattern of length ?
Not quite. Try another option.
Answer:
KMP preprocesses the pattern in time and searches the text in time.
Question 15 of 60 · Knuth-Morris-Pratt algorithm · Easy
Which part of the input is used to build the KMP LPS table?
Not quite. Try another option.
Answer: The pattern
The LPS table is computed from prefix and suffix relationships within the pattern.
Question 16 of 60 · Boyer-Moore algorithm · Easy
In which direction does Boyer-Moore typically compare pattern characters?
Not quite. Try another option.
Answer: From right to left
Boyer-Moore normally begins comparisons at the rightmost character of the aligned pattern.
Question 17 of 60 · Boyer-Moore algorithm · Easy
Which two heuristics are associated with the Boyer-Moore algorithm?
Not quite. Try another option.
Answer: Bad-character and good-suffix heuristics
Boyer-Moore uses bad-character and good-suffix information to choose efficient shifts.
Question 18 of 60 · Boyer-Moore algorithm · Easy
What information does the bad-character heuristic use after a mismatch?
Not quite. Try another option.
Answer: The mismatching text character
The bad-character heuristic uses the mismatching text character to determine a useful shift.
Question 19 of 60 · Boyer-Moore algorithm · Easy
What information does the good-suffix heuristic use?
Not quite. Try another option.
Answer: A suffix that matched before the mismatch
The good-suffix heuristic shifts the pattern based on a suffix that matched before a mismatch occurred.
Question 20 of 60 · Boyer-Moore algorithm · Easy
Why can Boyer-Moore be faster than brute-force matching in practice?
Not quite. Try another option.
Answer: It can shift the pattern by multiple positions
Its heuristics often allow several text positions to be skipped after a mismatch.
Question 21 of 60 · Design of algorithms for pattern matching problems · Medium
A text has length and a pattern has length . How many valid alignments must an exact pattern-matching algorithm potentially examine?
Not quite. Try another option.
Answer:
The pattern can begin at positions through , giving valid alignments.
Question 22 of 60 · Design of algorithms for pattern matching problems · Medium
Which algorithm is most suitable when a text arrives as a stream and previously read text characters should not be revisited?
Not quite. Try another option.
Answer: Knuth-Morris-Pratt matching
KMP processes the text from left to right without moving the text pointer backward, making it suitable for streaming input.
Question 23 of 60 · Design of algorithms for pattern matching problems · Medium
A case-insensitive matcher converts both the text and pattern to lowercase before searching. Why must the same normalization be applied to both?
Not quite. Try another option.
Answer: To preserve consistent equality comparisons
Applying the same normalization ensures that corresponding text and pattern characters are compared under the same rules.
Question 24 of 60 · Design of algorithms for pattern matching problems · Medium
If every occurrence, including overlapping occurrences, must be reported, what should a correct pattern-matching algorithm do after finding a match?
Not quite. Try another option.
Answer: Continue from a valid next alignment
The search must continue from an alignment that can still contain a match; otherwise, overlapping occurrences may be missed.
Question 25 of 60 · Brute-force algorithm · Medium
Using brute-force matching on the text AABAACAADAABAABA with pattern AABA, which zero-based starting positions are reported?
Not quite. Try another option.
Answer:
Direct comparison at every alignment finds AABA beginning at indices , , and .
Question 26 of 60 · Brute-force algorithm · Medium
A 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?
Not quite. Try another option.
Answer:
There are alignments, and all three characters match at each one, so the total is comparisons.
Question 27 of 60 · Brute-force algorithm · Medium
What is the worst-case running time of brute-force pattern matching for text length and pattern length ?
Not quite. Try another option.
Answer:
In the worst case, up to characters are compared at each of approximately alignments, giving time.
Question 28 of 60 · Brute-force algorithm · Medium
After a mismatch at an alignment, how does the standard brute-force algorithm choose the next alignment?
Not quite. Try another option.
Answer: It shifts the pattern one position right
The standard brute-force method tests every possible alignment, so it advances the pattern by exactly one position after a mismatch.
Question 29 of 60 · Knuth-Morris-Pratt algorithm · Medium
What is the LPS array for the pattern ABABAC?
Not quite. Try another option.
Answer:
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.
Question 30 of 60 · Knuth-Morris-Pratt algorithm · Medium
During 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?
Not quite. Try another option.
Answer: Matching resumes at pattern index
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.
Question 31 of 60 · Knuth-Morris-Pratt algorithm · Medium
What does an LPS value of at pattern index mean?
Not quite. Try another option.
Answer: A length- proper prefix is also a suffix
LPS stores the length of the longest proper prefix of the pattern prefix ending at that is also its suffix.
Question 32 of 60 · Knuth-Morris-Pratt algorithm · Medium
KMP searches for ABABCABAB in ABABDABACDABABCABAB. At which zero-based index does the pattern begin?
Not quite. Try another option.
Answer:
The substring from text index through is exactly ABABCABAB.
Question 33 of 60 · Knuth-Morris-Pratt algorithm · Medium
After KMP finds AAA in AAAAA, which pattern index should it use next to detect overlapping matches?
Not quite. Try another option.
Answer:
The final LPS value for AAA is , so KMP preserves the two-character overlap and can find matches at indices , , and .
Question 34 of 60 · Knuth-Morris-Pratt algorithm · Medium
Including preprocessing, what is the asymptotic time complexity of KMP for a text of length and a pattern of length ?
Not quite. Try another option.
Answer:
Constructing the LPS array takes time, and scanning the text takes time.
Question 35 of 60 · Boyer-Moore algorithm · Medium
In which direction does the Boyer-Moore algorithm normally compare pattern characters at each alignment?
Not quite. Try another option.
Answer: From right to left
Boyer-Moore begins with the pattern's rightmost character, enabling its shift heuristics to skip multiple alignments.
Question 36 of 60 · Boyer-Moore algorithm · Medium
For 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?
Not quite. Try another option.
Answer:
The bad-character shift is .
Question 37 of 60 · Boyer-Moore algorithm · Medium
A 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?
Not quite. Try another option.
Answer:
An absent character is treated as having last position , so the shift is .
Question 38 of 60 · Boyer-Moore algorithm · Medium
What information is used by the good-suffix rule after a mismatch?
Not quite. Try another option.
Answer: The suffix already matched at the alignment
The good-suffix rule shifts the pattern using another occurrence of the matched suffix or a prefix that matches part of that suffix.
Question 39 of 60 · Boyer-Moore algorithm · Medium
If the bad-character rule suggests a shift of and the good-suffix rule suggests a shift of , what shift does standard Boyer-Moore apply?
Not quite. Try another option.
Answer:
Boyer-Moore normally takes the larger valid shift supplied by its heuristics, so it shifts by .
Question 40 of 60 · Boyer-Moore algorithm · Medium
Why can Boyer-Moore be especially effective on long patterns over a large alphabet?
Not quite. Try another option.
Answer: Its heuristics can skip several alignments
Bad-character and good-suffix shifts often move the pattern several positions, reducing comparisons in typical inputs.
Question 41 of 60 · Design of algorithms for pattern matching problems · Hard
Let 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 ?
Not quite. Try another option.
Answer:
The match ends at combined-string index . Since begins at index , its starting index is .
Question 42 of 60 · Design of algorithms for pattern matching problems · Hard
For two nonempty strings and of equal length , which test correctly determines in time whether is a cyclic rotation of ?
Not quite. Try another option.
Answer: Search for in and accept a match starting in .
Every rotation of appears in . Restricting the start to excludes the duplicate position .
Question 43 of 60 · Design of algorithms for pattern matching problems · Hard
A 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?
Not quite. Try another option.
Answer: Preserve KMP's matched-prefix length between chunks and preprocess the pattern once.
KMP's state summarizes the relevant suffix of all processed text. Carrying across chunks detects boundary-spanning matches in total time.
Question 44 of 60 · Design of algorithms for pattern matching problems · Hard
Assume and arbitrary unstructured inputs. What is the optimal worst-case asymptotic time for reporting all occurrences of one length- pattern in a length- text?
Not quite. Try another option.
Answer: ; both inputs may require inspection, and KMP attains this bound.
An adversary can force inspection of the pattern and text, giving . KMP matches this with time.
Question 45 of 60 · Design of algorithms for pattern matching problems · Hard
An occurrence is formally defined by for every integer . Under this definition, how should an algorithm handle an empty pattern when the text has length ?
Not quite. Try another option.
Answer: Report all boundaries from position through position .
For , the permitted starts satisfy . An explicit empty-pattern case also prevents zero-length shifts or infinite loops.
Question 46 of 60 · Brute-force algorithm · Hard
A left-to-right brute-force matcher searches for , where . How many character comparisons are performed when every alignment is examined?
Not quite. Try another option.
Answer:
At each of the alignments, the first symbols match and the final comparison fails, producing exactly comparisons.
Question 47 of 60 · Brute-force algorithm · Hard
A left-to-right brute-force algorithm reports every match of in . Counting both successful and failed character tests, how many comparisons does it perform?
Not quite. Try another option.
Answer: comparisons
Alignments use four comparisons each, while alignments fail on their first comparisons. The total is .
Question 48 of 60 · Brute-force algorithm · Hard
Suppose 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?
Not quite. Try another option.
Answer:
The th comparison occurs only if the preceding symbols match, with probability . Summing this geometric series over all alignments gives the result.
Question 49 of 60 · Brute-force algorithm · Hard
For 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?
Not quite. Try another option.
Answer:
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 .
Question 50 of 60 · Brute-force algorithm · Hard
After 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?
Not quite. Try another option.
Answer: Patterns having no nonempty proper prefix that is also a suffix.
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.
Question 51 of 60 · Knuth-Morris-Pratt algorithm · Hard
Using the convention that is the length of the longest proper prefix that is also a suffix of , what is the prefix array for ?
Not quite. Try another option.
Answer:
At the third consecutive , fallback from border length leaves border , which extends to . The final then extends that border to .
Question 52 of 60 · Knuth-Morris-Pratt algorithm · Hard
For , KMP is in state , meaning that has matched. If the next text symbol is , what state does KMP enter after all necessary fallback operations?
Not quite. Try another option.
Answer:
The expected symbol mismatches, so KMP falls from to . The new symbol then matches , producing .
Question 53 of 60 · Knuth-Morris-Pratt algorithm · Hard
KMP searches for . Which result and post-match state are correct when overlapping occurrences must be reported?
Not quite. Try another option.
Answer: Report starts and set after each match.
The pattern's longest proper border is of length . Retaining that state after a match allows KMP to detect occurrences shifted by two positions.
Question 54 of 60 · Knuth-Morris-Pratt algorithm · Hard
Which amortization fact is sufficient to prove that KMP's text scan takes time even though one text symbol may trigger several fallback operations?
Not quite. Try another option.
Answer: Total fallback decreases of are bounded by prior increases of .
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.
Question 55 of 60 · Knuth-Morris-Pratt algorithm · Hard
For , the final prefix value is . Which sequence lists all nonempty proper border lengths from longest to shortest?
Not quite. Try another option.
Answer:
Repeatedly following gives , then , then , and finally .
Question 56 of 60 · Boyer-Moore algorithm · Hard
In 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?
Not quite. Try another option.
Answer: positions
The rightmost in the pattern is at index . The bad-character shift is .
Question 57 of 60 · Boyer-Moore algorithm · Hard
After Boyer-Moore matches the suffix but mismatches at , which action describes the strong good-suffix rule?
Not quite. Try another option.
Answer: Align the rightmost earlier preceded by a symbol different from ; otherwise align the longest suffix of that is a prefix of .
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.
Question 58 of 60 · Boyer-Moore algorithm · Hard
After a complete match of , what shift does Boyer-Moore's good-suffix preprocessing prescribe to preserve overlapping matches?
Not quite. Try another option.
Answer: positions
The longest proper border is of length . A complete-match shift is , allowing the next overlapping occurrence.
Question 59 of 60 · Boyer-Moore algorithm · Hard
Consider Boyer-Moore using only the standard bad-character rule, scanning right to left. For and , what is its worst-case comparison count?
Not quite. Try another option.
Answer:
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.
Question 60 of 60 · Boyer-Moore algorithm · Hard
A 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?
Not quite. Try another option.
Answer: comparisons
Alignment starts are , the largest multiple of not exceeding . Their count is .
That's unit 8
Answer a few questions and your score shows up here.
Nearly through. Did unit 8 of ECAP538 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 8 of ECAP538 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.