Home Paper Deloitte National Level Assessment Coding Questions And Answers 2024

Deloitte National Level Assessment Coding Questions And Answers 2024

by Anup Maurya
23 minutes read

In this article, we’ll see all the question asked in Deloitte National Level Assessment Coding Questions And Answers 2024.

Question 1:

Write a program that takes a string as an input, string contains various words. Sort these words in ascending order (alphabetically).
After sorting the list of words print all the words which are starting with vowel with their positions.
Note :
There will be atleast one word in the list which will be starting with vowel.

Sample Input 1:
only god can judge me now
Sample Output 1:
only 6

Explanation:
Sorted words will look like below
a, Can, god, judge, me, now
only 6

Sample Input 2:
god can judge me now a
Sample Output 1:
a 1

Explanation:
Sorted words will look like below
Can, god, judge, me, now, only
a 1

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main() {
  string sentence;
  cout << "Enter a sentence: ";
  getline(cin, sentence);

  // 1. Split the sentence into words
  vector<string> words;
  string word;
  for (char c : sentence) {
    if (isspace(c)) {
      if (!word.empty()) {
        words.push_back(word);
      }
      word.clear();
    } else {
      word += c;
    }
  }
  if (!word.empty()) {
    words.push_back(word);
  }

  // 2. Convert words to lowercase for case-insensitive sorting
  for (string& word : words) {
    transform(word.begin(), word.end(), word.begin(), ::tolower);
  }

  // 3. Sort the words alphabetically
  sort(words.begin(), words.end());

  // 4. Count occurrences using an unordered_map
  unordered_map<string, int> word_counts;
  for (const string& word : words) {
    word_counts[word]++;
  }

  // 5. Print the sorted words and their counts
  cout << "Sorted words will look like below\n";
  for (const auto& [word, count] : word_counts) {
    cout << word << ": " << count << endl;
  }

  return 0;
}

related posts

Leave a Comment

Enable Notifications OK No thanks