Tesco

Online Assessment
Online Assessment//Vowel-Starting Words in Alphabetically Sorted List
Q1
Easy

Vowel-Starting Words in Alphabetically Sorted List

StringsSortingImplementation
Set 1 • min
Problem Statement

Write a program that takes a list of words as input and sorts the list of words in alphabetically ascending order. Identify words in the sorted list that start with a vowel (a, e, i, o, u) regardless of their case.

Display each of these words along with their 1-based positions in the sorted list, separated by a single space, each on a separate line.

If no word in the list starts with a vowel, print 0 as the output.

Input Format
  • ▸The first line of input contains a list of words with each separated by a single space.
Output Format
  • ▸Print each vowel-starting word and its 1-based position in the sorted list separated by a single space, each on a separate line. If no word starts with a vowel, print 0.
Constraints
  • 1 <= Number of words <= 1000
  • 1 <= Length of each word <= 100
  • Words consist of uppercase and lowercase English letters.
Sample Test Cases
Example 1
Input
only god can judge me now
Output
only 6
Explanation

Given list: ["only", "god", "can", "judge", "me", "now"].

Alphabetically sorted list:

1: can

2: god

3: judge

4: me

5: now

6: only

The word "only" starts with the vowel 'o' and is at position 6 (1-based index). Hence the output is 'only 6'.

Example 2
Input
apple banana elephant orange
Output
apple 1
elephant 3
orange 4
Explanation

Sorted list: ["apple", "banana", "elephant", "orange"]. Words starting with vowels are at positions 1, 3, and 4.

Example 3
Input
rhythm sky crypt
Output
0
Explanation

None of the words start with a vowel, so 0 is printed.

Expected Approach

1. Read all space-separated words from the input line. 2. Sort the list of words in standard ascending alphabetical order. 3. Iterate through the sorted list (using 1-based index i from 1 to N): - Check if the first character of word[i-1] is a vowel (a, e, i, o, u, case-insensitive). - If it is a vowel, print `<word> <i>` and mark a boolean flag `found = true`. 4. If `found` remains false after checking all words, print `0`. 5. Runs in O(N log N * L) time and O(N * L) space.