0% found this document useful (0 votes)
8 views6 pages

Grouping Words, Students, and Cities by Initials

The document provides solutions for grouping words, student names, and city names by their first letter into an object where keys are uppercase letters and values are arrays of corresponding words or names. It includes example inputs and outputs, as well as explanations of the time and space complexity for each solution. Additionally, it offers information on various educational resources and classes available for learning programming and development skills.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Grouping Words, Students, and Cities by Initials

The document provides solutions for grouping words, student names, and city names by their first letter into an object where keys are uppercase letters and values are arrays of corresponding words or names. It includes example inputs and outputs, as well as explanations of the time and space complexity for each solution. Additionally, it offers information on various educational resources and classes available for learning programming and development skills.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Real 49:

1. Group Words Alphabetically:

Problem:

You are given an array of words. Your task is to group these words alphabetically based on
their first letter and return the result as an object (dictionary/map), where:

• The keys are uppercase letters (A-Z).


• The values are arrays containing words that start with the corresponding letter.

Input:
const words = [
"Apple",
"Animals",
"Bat",
"Ball",
"Cat",
"Candy",
"Dog",
"Dare",
"Earth",
"Exam",
"Gang",
"Gun",
"Hat",
"Hate",
];
[Link](groupWordsByAlphabet(words));

Output:
{
"A": ["Animals", "Apple"],
"B": ["Ball", "Bat"],
"C": ["Candy", "Cat"],
"D": ["Dare", "Dog"],
"E": ["Earth", "Exam"],
"G": ["Gang", "Gun"],
"H": ["Hat", "Hate"]
}

Solution:
function groupWordsByAlphabet(words) {
let wordGroups = {};
for (let word of words) {
let firstLetter = word[0].toUpperCase(); // Get the first letter in uppercase

if (!wordGroups[firstLetter]) {
wordGroups[firstLetter] = []; // Initialize array if not exists
}

wordGroups[firstLetter].push(word); // Add word to respective group


}

return wordGroups;
}

// 🔍 Test Case
const words = [
"Apple",
"Animals",
"Bat",
"Ball",
"Cat",
"Candy",
"Dog",
"Dare",
"Earth",
"Exam",
"Gang",
"Gun",
"Hat",
"Hate",
];
[Link](groupWordsByAlphabet(words));

Explanation:
• The function loops through the array, extracts the first letter, and groups words into
an object.
• Each key represents the starting letter, and each value is an array of words beginning
with that letter.
• Time Complexity: O(n) (Iterate once through the array)
• Space Complexity: O(n) (Storing words in an object)

2. Group Students by First Letter of Their Name

Problem:
You are given an array of student names. Group these names based on the first letter of each
name. The result should be an object where:

• Keys are uppercase letters


• Values are arrays of names starting with that letter

const students = ["Amit", "Anjali", "Bhanu", "Bharat", "Chetan", "Charu"];


[Link](groupStudentsByInitial(students));

Solution:
function groupStudentsByInitial(students) {
const grouped = {};

for (let name of students) {


const firstChar = name[0].toUpperCase();
if (!grouped[firstChar]) {
grouped[firstChar] = [];
}
grouped[firstChar].push(name);
}

return grouped;
}

Explanation:

• We loop through the students array.


• For each name, get its first character and use it as the key.
• Group names by their first character.
• Time: O(n), Space: O(n)

3. Group Cities by Starting Letter

Problem:

Given an array of city names in India, group them by their starting letter into an object where
keys are the uppercase letters and values are the cities that begin with that letter.

const cities = [
"Delhi",
"Dehradun",
"Mumbai",
"Meerut",
"Chennai",
"Coimbatore",
];
[Link](groupCitiesByAlphabet(cities));

Solution:
function groupCitiesByAlphabet(cities) {
const cityGroups = {};

for (let city of cities) {


const key = city[0].toUpperCase();
if (!cityGroups[key]) {
cityGroups[key] = [];
}
cityGroups[key].push(city);
}

return cityGroups;
}

Explanation:

• Cities are grouped by their first letter.


• If the key doesn't exist, we initialize it.
• Append city names to the appropriate array.

For More Interview Questions:

Get Your PDF Files Instantly:

Structured learning made simple! Choose from our affordable guides:

1. JavaScript & DSA with 700+ Questions Interview & Answers ➔ ₹150/- (Price
may increase with weekly updates!)

100+ JavaScript theory questions to master core concepts


60+ output-based questions to test JavaScript behavior

50+ coding tasks with detailed explanations


550+ LeetCode & Hackathon coding challenges covering interview Q’s from
FAANG & top product-based companies
Weekly updates with coding questions

2. JavaScript 200+ Coding Interview Questions & Answers ➔ ₹60/-

100+ JavaScript theory questions to master core concepts


60+ output-based questions to test JavaScript behavior

50+ coding tasks with detailed explanations

3. React Coding Interview Questions & Answers ➔ ₹50/-


→ Covers essential React concepts and problem-solving techniques.
4. Backend Development Questions & Answers ➔ ₹60/-
→ Focused on [Link], [Link], MongoDB, and related technologies.

How to Get It?

• Message us on WhatsApp: 9398123134


• Pay the respective amount, and receive your PDF instantly!

Instant replies are available between 8:00 AM to 10:00 PM IST.

If you do not receive a reply within 10 minutes on WhatsApp or if you need a PDF
outside of these hours, please visit:
[Link]

Telugu Full Stack Web Development Recording Classes Available!

Learn in Telugu with structured, high-quality recorded classes!

What You Get:

• Lifetime Access to Videos


• Complete Source Code (GitHub Access)
• Interview Questions and Answers

• HTML, CSS, JavaScript ➔ ₹1500/-

Demo: [Link]
• ReactJS ➔ ₹1500/-

Demo: [Link]

• [Link], [Link], MongoDB ➔ ₹1500/-

Demo: [Link]

Message us on WhatsApp 9398123134 to get access!

Join Our Online and Offline Classes

Upskill with confidence—our courses come with a


100% PLACEMENT GUARANTEE!

1. Frontend Technologies
Learn ReactJS, and Angular.
2. Full Stack Development
Master MERN stack, Java, Python, or .NET technologies.
3. Data Science and Analytics
Gain skills in data visualization, modeling, and analysis.
4. Artificial Intelligence and Machine Learning
Explore AI tools, concepts, and practical implementations.
5. Advanced Topics
o Cloud Computing: AWS, Azure
o Cybersecurity: Ethical hacking, penetration testing
6. Power BI

Call us at 9398123134 to get started today!

You might also like