Question 41
MediumWhat is the output of this code?
def lps(p):
t = [] * (p)
k =
i (, (p)):
k p[i] != p[k]:
k = t[k - ]
p[i] == p[k]:
k +=
t[i] = k
t
(lps())
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is the output of this code?
def lps(p):
t = [] * (p)
k =
i (, (p)):
k p[i] != p[k]:
k = t[k - ]
p[i] == p[k]:
k +=
t[i] = k
t
(lps())
[0, 0, 1, 2, 2, 3, 4]
[0, 1, 0, 1, 2, 2, 3]
[0, 1, 2, 0, 1, 2, 3]
[0, 1, 0, 1, 2, 3, 3]
Sign up free to see the answer and a step-by-step explanation.
How can you check whether two strings of length n are anagrams in O(n) time?
Sort both and compare (O(n) always)
Compare them character by character
Reverse one and compare
Count the characters of each with an array or hash map and compare the counts
Sign up free to see the answer and a step-by-step explanation.
How can you check whether s2 is a rotation of s1 (same length)?
Reverse s1 and compare
Sort both
Compare the first characters
Check whether s2 is a substring of s1 + s1
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of the naive pattern search for a pattern of length m in a text of length n?
O(n)
O(log n)
O((n − m + 1) × m)
O(n + m)
Sign up free to see the answer and a step-by-step explanation.
Which technique finds the longest palindromic substring in O(n²) time and O(1) space?
Expanding around each centre (2n − 1 centres)
Using a stack
Sorting the string
Binary search
Sign up free to see the answer and a step-by-step explanation.
Why are strings immutable in Java and Python?
Safe sharing, caching of hash codes and use as dictionary keys
To save typing
To allow multiple inheritance
Because they are stored on disk
Sign up free to see the answer and a step-by-step explanation.
Which class should be used to build a long string in a loop in Java?
Character
StringBuilder
String
StringTokenizer
Sign up free to see the answer and a step-by-step explanation.
How many substrings (including the full string, excluding the empty one) does a string of length n have?
2ⁿ
n²
n(n + 1)/2
n!
Sign up free to see the answer and a step-by-step explanation.
How many subsequences (including the empty one) does a string of n distinct characters have?
2ⁿ
n²
n!
n(n + 1)/2
Sign up free to see the answer and a step-by-step explanation.
What is the edit (Levenshtein) distance between 'kitten' and 'sitting'?
3
5
2
4
Sign up free to see the answer and a step-by-step explanation.