Complete String Problem Statement
Given an array of strings A
of size N
, determine the longest complete string. A string is deemed complete if every prefix of the string also appears in the array. If multiple strings of the same maximal length exist, return the lexicographically smallest one. If no complete string exists, return "None"
.
Input:
First line contains an integer 'T' denoting the number of test cases. Each test case includes:
An integer N
indicating the size of array A
.
A list of N
space-separated strings as elements of A
.
Output:
For each test case, output the longest complete string in 'A'. If multiple strings of the same length exist, return the smallest lexicographically. If no string qualifies, return "None"
.
Example:
N = 4
A = [ "ab" , "abc" , "a" , "bp" ]
Explanation:
Prefixes of string "a" is “a” which is in 'A'.
Prefixes of "ab" are "a", "ab", both in 'A'.
"bp" has prefix "b" not in 'A'.
"abc" has prefixes "a", "ab", "abc", all in 'A'.
Thus, "abc" is the longest complete string.
Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 100000
1 ≤ A[i].length ≤ 100000
- The characters in
A[i]
are lowercase English letters. - Sum of all
A[i].length
across test cases ≤ 100000
Note:
No need to print anything, just implement the function to get the output.

AnswerBot
4mo
Find the longest complete string in an array of strings, where every prefix of the string also appears in the array.
Iterate through each string in the array and check if all its prefixes exist in the ...read more
Help your peers!
Add answer anonymously...
ThoughtWorks Software Engineer interview questions & answers
A Software Engineer was asked 5mo agoQ. Explain cloud concepts.
A Software Engineer was asked Q. Which tech stack would you use for which case?
A Software Engineer was asked Q. Describe a real-time situation and how you would code it using OOP concepts.
Popular interview questions of Software Engineer
A Software Engineer was asked 5mo agoQ1. Explain cloud concepts.
A Software Engineer was asked Q2. Describe a real-time situation and how you would code it using OOP concepts.
A Software Engineer was asked Q3. Write a program to print the HCF of numbers as output.
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

