Prepin
Log in

Locate a triplet in the provided array that is sorted.

Approach / Explanation

The interview question, Find a sorted triplet in the given array , requires you to develop code that identifies a sorted increasing subsequence of three integers within an array. The term "triplet" refers to any three numbers from the array that are arranged in ascending order and appear in that sequence. It's possible that there may be no such triplet present; if this occurs, you should explain how you would address this situation in your answer. When formulating your response, consider the following key points: Knowledge of data structures, particularly arrays. Skill in sorting algorithms. Consideration of edge cases (such as the absence of triplets). Effective time and space management in your solution.

Suggested Answer

One way to tackle this problem is by initializing two arrays of the same size. The first array will hold the smallest number to the left of each index, while the second array will contain the largest number to the right of each index. The next step is to iterate through the data and update both arrays accordingly. Finally, check for any index where the corresponding values in both arrays are respectively greater and smaller than the number at that index. For instance, given the array {12, 11, 10, 5, 6, 2, 30}, there are multiple triplets that can be identified, such as {5, 6, 30}, {10, 11, 12}, and {5, 6, 2, 30}, among others. Any of these triplets can be returned since they are all valid.

Alternative Answer

As an alternative, you might consider a different method. Begin by initializing three pointers: the first one at the beginning of the array, the second at the position right after the first, and the third at the position right after the second. Keep the second pointer fixed, and check if the value at the third pointer is greater than the value at the second pointer, and if the value at the second pointer is greater than the value at the first pointer. If both conditions are met, you have identified a valid triplet. If not, adjust the pointers as follows: if the value at the third pointer is not greater than that at the second, move the third pointer forward. If the value at the second pointer is not greater than that at the first, move the second pointer forward. If none of these conditions apply, move the first pointer forward. For instance, in the array {1, 4, 3, 7, 2, 1, 5, 6}, one possible sorted triplet is {1, 2, 5}, which can be located using this method. It's important to note that the goal is not only to arrive at a correct solution but to ensure that the solution is efficient. Therefore, take into account the time complexity and space usage of your approach.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.