Find the third-largest word by length
Problem Description
Given an array of strings, strArr, where the array's length is guaranteed to be at least 3, the task is to identify and return the third largest word based on its string length.
Ranking Rules
Words are ranked in descending order of their string length. Specific rules apply:
- Length Priority: A word with a greater length is considered to have a higher rank.
- Tie-breaking (Stability): If two or more words share the same length, their original relative order from the input array must be preserved. This implies a stable sort by length in descending order.
- Target: After ranking, the word at rank 3 (i.e., the third element in the sorted sequence) should be returned.
Assumptions and Constraints
- Each string within
strArrwill consist solely of letters. - The input array
strArrwill always contain at least 3 strings.
Examples
- Input:
["hello", "world", "before", "all"]- Output:
"world"
- Output:
- Input:
["hello", "world", "after", "all"]- Output:
"after"
- Output:
- Input:
["coder", "byte", "code"]- Output:
"code"
- Output:
- Input:
["abc", "defg", "z", "hijk"]- Output:
"abc"
- Output:
AI interview prep
Core dilemma
Efficiently finding the Kth largest element (specifically the 3rd) while strictly adhering to stability requirements for tie-breaking, without necessarily sorting the entire dataset.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Find the third-largest word by length
Problem Description
Given an array of strings, strArr, where the array's length is guaranteed to be at least 3, the task is to identify and return the third largest word based on its string length.
Ranking Rules
Words are ranked in descending order of their string length. Specific rules apply:
- Length Priority: A word with a greater length is considered to have a higher rank.
- Tie-breaking (Stability): If two or more words share the same length, their original relative order from the input array must be preserved. This implies a stable sort by length in descending order.
- Target: After ranking, the word at rank 3 (i.e., the third element in the sorted sequence) should be returned.
Assumptions and Constraints
- Each string within
strArrwill consist solely of letters. - The input array
strArrwill always contain at least 3 strings.
Examples
- Input:
["hello", "world", "before", "all"]- Output:
"world"
- Output:
- Input:
["hello", "world", "after", "all"]- Output:
"after"
- Output:
- Input:
["coder", "byte", "code"]- Output:
"code"
- Output:
- Input:
["abc", "defg", "z", "hijk"]- Output:
"abc"
- Output: