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

Given an array of strings, find the longest complete string 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 t...read more

Help your peers!
Select
Add answer anonymously...

Nagarro Software Developer interview questions & answers

A Software Developer was asked 8mo agoQ. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ. Is the directory a file?
A Software Developer was asked 9mo agoQ. What is memory segmentation?

Popular interview questions of Software Developer

A Software Developer was asked 8mo agoQ1. Explain Kafka and how you would implement it.
A Software Developer was asked 9mo agoQ2. Is the directory a file?
A Software Developer was asked 9mo agoQ3. What is memory segmentation?
Nagarro Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits