Longest Common Prefix Problem Statement

You are given an array ‘ARR’ consisting of ‘N’ strings. Your task is to find the longest common prefix among all these strings. If there is no common prefix, you have to return an empty string.

Input:
The first line of the input contains a single integer, 'T,' denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of strings in the array.
The next line contains ‘N’ space-separated strings denoting the elements of the array ‘ARR’.
Output:
For each test case, print a single string corresponding to the longest common prefix.
Print the output of each test case in a separate line.
Example:
Input:
ARR = [“coding”, ”codezen”, ”codingninja”, ”coders”]
Output:
"cod"
Explanation:

The longest common prefix among all the given strings is “cod” as it is present as a prefix in all strings. Hence, the answer is “cod”.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 3000
  • 1 <= |ARR[i]| <= 1000
  • Each string consists of only lowercase letters.
  • Time limit: 1 sec
Note:

You do not need to print anything. It has already been taken care of. Just implement the given function.

AnswerBot
4mo

Find the longest common prefix among an array of strings.

  • Iterate through the characters of the first string and compare with corresponding characters of other strings.

  • Stop when a character doesn't mat...read more

Vaishu Sumthankar
3y

Binary Search In this approach, we will first find the string with the minimum length among all the strings in the array and store it as prefix because the shortest string is the longest possible com...read more

Help your peers!
Select
Add answer anonymously...

Amazon Program Analyst interview questions & answers

A Program Analyst was asked Q. Longest Common Prefix Problem Statement You are given an array ‘ARR’ consisting ...read more
A Program Analyst was asked Q. Longest Common Subsequence Problem Statement Given two strings STR1 and STR2, de...read more
A Program Analyst was asked Q. Closest Sum Problem Statement Given an array of integers ARR of size N and an in...read more

Popular interview questions of Program Analyst

A Program Analyst was asked Q1. Longest Common Prefix Problem Statement You are given an array ‘ARR’ consisting ...read more
A Program Analyst was asked Q2. Longest Common Subsequence Problem Statement Given two strings STR1 and STR2, de...read more
A Program Analyst was asked Q3. Closest Sum Problem Statement Given an array of integers ARR of size N and an in...read more
Amazon Program Analyst 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