Software Development Exam  >  Software Development Tests  >  DSA in C++  >  Test: Tries - 2 - Software Development MCQ

Test: Tries - 2 - Software Development MCQ


Test Description

15 Questions MCQ Test DSA in C++ - Test: Tries - 2

Test: Tries - 2 for Software Development 2024 is part of DSA in C++ preparation. The Test: Tries - 2 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Tries - 2 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Tries - 2 below.
Solutions of Test: Tries - 2 questions in English are available as part of our DSA in C++ for Software Development & Test: Tries - 2 solutions in Hindi for DSA in C++ course. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free. Attempt Test: Tries - 2 | 15 questions in 30 minutes | Mock test for Software Development preparation | Free important questions MCQ to study DSA in C++ for Software Development Exam | Download free PDF with solutions
Test: Tries - 2 - Question 1

What is the time complexity of inserting a word into a TRIE with N nodes?

Detailed Solution for Test: Tries - 2 - Question 1

The time complexity of inserting a word into a TRIE is O(M), where M is the length of the word. This is because we need to traverse the TRIE for each character in the word.

Test: Tries - 2 - Question 2

Which operation is NOT typically performed on a TRIE?

Detailed Solution for Test: Tries - 2 - Question 2

Sorting is not typically performed on a TRIE. TRIEs are primarily used for efficient search and insert operations, not for sorting elements.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Tries - 2 - Question 3

Which of the following is NOT a disadvantage of using a TRIE?

Detailed Solution for Test: Tries - 2 - Question 3

TRIEs are efficient for handling large datasets, especially when it comes to searching for words or prefixes. They have a time complexity of O(M), where M is the length of the word being searched, which makes them suitable for large datasets.

Test: Tries - 2 - Question 4

Which of the following problems can be efficiently solved using TRIE?

Detailed Solution for Test: Tries - 2 - Question 4

TRIEs can efficiently count the number of occurrences of words in a large text. By traversing the TRIE and keeping track of the count at each end-of-word node, we can quickly determine the frequency of each word.

Test: Tries - 2 - Question 5

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

int main() {
    TrieNode* root = new TrieNode();
    root->children['a'] = new TrieNode();
    root->children['b'] = new TrieNode();
    root->children['a']->children['c'] = new TrieNode();
    root->children['a']->children['c']->isEndOfWord = true;
    root->children['b']->children['d'] = new TrieNode();
    root->children['b']->children['d']->isEndOfWord = true;
    std::cout << (root->children['a']->children['c']->isEndOfWord ? "True" : "False") << std::endl;
    std::cout << (root->children['b']->children['d']->isEndOfWord ? "True" : "False") << std::endl;
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 5

The output of the code will be "True" because the word "hello" is inserted into the TRIE using the 'insert' function, and the 'search' function returns true for the word "hello".

Test: Tries - 2 - Question 6

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

void insert(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            current->children[ch] = new TrieNode();
        }
        current = current->children[ch];
    }
    current->isEndOfWord = true;
}

bool search(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            return false;
        }
        current = current->children[ch];
    }
    return current->isEndOfWord;
}

int main() {
    TrieNode* root = new TrieNode();
    insert(root, "hello");
    insert(root, "world");
    std::cout << (search(root, "hello") ? "True" : "False") << std::endl;
    std::cout << (search(root, "world") ? "True" : "False") << std::endl;
    std::cout << (search(root, "bye") ? "True" : "False") << std::endl;
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 6

The output of the code will be "False" for the first search because the word "bye" is not present in the TRIE. The output will be "True" for the second search because the word "world" is present in the TRIE. The output will be "False" for the third search because the word "bye" is not present in the TRIE.

Test: Tries - 2 - Question 7

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

void insert(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            current->children[ch] = new TrieNode();
        }
        current = current->children[ch];
    }
    current->isEndOfWord = true;
}

bool deleteWord(TrieNode* root, std::string word, int index) {
    if (index == word.length()) {
        if (!root->isEndOfWord)
            return false;
        root->isEndOfWord = false;
        return root->children.empty();
    }

    char ch = word[index];
    if (root->children.find(ch) == root->children.end())
        return false;

    TrieNode* childNode = root->children[ch];
    bool shouldDeleteCurrentNode = deleteWord(childNode, word, index + 1);

    if (shouldDeleteCurrentNode) {
        root->children.erase(ch);
        delete childNode;
        return root->children.empty();
    }

    return false;
}

int main() {
    TrieNode* root = new TrieNode();
    insert(root, "hello");
    insert(root, "world");
    deleteWord(root, "hello", 0);
    std::cout << (search(root, "hello") ? "True" : "False") << std::endl;
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 7

The output of the code will be "False" because the 'deleteWord' function is called to delete the word "hello" from the TRIE, and then the 'search' function is called to check if the word "hello" is still present in the TRIE. Since it was deleted, the output will be "False".

Test: Tries - 2 - Question 8

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

void insert(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            current->children[ch] = new TrieNode();
        }
        current = current->children[ch];
    }
    current->isEndOfWord = true;
}

void weightedPrefixSearch(TrieNode* root, std::string prefix, int weight) {
    if (root == nullptr)
        return;

    if (root->isEndOfWord) {
        std::cout << prefix << " (Weight: " << weight << ")" << std::endl;
    }

    for (auto& pair : root->children) {
        char ch = pair.first;
        TrieNode* childNode = pair.second;
        weightedPrefixSearch(childNode, prefix + ch, weight + ch - 'a' + 1);
    }
}

int main() {
    TrieNode* root = new TrieNode();
    insert(root, "apple");
    insert(root, "banana");
    insert(root, "cherry");
    insert(root, "cat");
    weightedPrefixSearch(root, "", 0);
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 8

The output of the code will be "True" because the 'deleteWord' function is called to delete the word "hello" from the TRIE. Since "hello" was successfully deleted, the 'search' function will return true.

Test: Tries - 2 - Question 9

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

void insert(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            current->children[ch] = new TrieNode();
        }
        current = current->children[ch];
    }
    current->isEndOfWord = true;
}

int countWords(TrieNode* root) {
    int count = 0;
    if (root->isEndOfWord)
        count++;

    for (auto& pair : root->children) {
        TrieNode* childNode = pair.second;
        count += countWords(childNode);
    }

    return count;
}

int main() {
    TrieNode* root = new TrieNode();
    insert(root, "hello");
    insert(root, "world");
    std::cout << countWords(root) << std::endl;
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 9

The 'countWords' function counts the number of words in the TRIE. In this case, "hello" and "world" are inserted into the TRIE using the 'insert' function, so the output will be 2.

Test: Tries - 2 - Question 10

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

void insert(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            current->children[ch] = new TrieNode();
        }
        current = current->children[ch];
    }
    current->isEndOfWord = true;
}

bool hasChildren(TrieNode* root) {
    return !root->children.empty();
}

bool deleteHelper(TrieNode* root, std::string word, int index) {
    if (index == word.length()) {
        if (!root->isEndOfWord)
            return false;
        root->isEndOfWord = false;
        return !hasChildren(root);
    }

    char ch = word[index];
    if (root->children.find(ch) == root->children.end())
        return false;

    TrieNode* childNode = root->children[ch];
    bool shouldDeleteCurrentNode = deleteHelper(childNode, word, index + 1);

    if (shouldDeleteCurrentNode) {
        root->children.erase(ch);
        delete childNode;
        return !hasChildren(root);
    }

    return false;
}

bool deleteWord(TrieNode* root, std::string word) {
    return deleteHelper(root, word, 0);
}

int main() {
    TrieNode* root = new TrieNode();
    insert(root, "hello");
    insert(root, "world");
    deleteWord(root, "hello");
    std::cout << countWords(root) << std::endl;
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 10

The 'deleteWord' function is called to delete the word "hello" from the TRIE. After deletion, the 'countWords' function is called to count the number of words in the TRIE. Since "hello" was deleted, the output will be 0.

Test: Tries - 2 - Question 11

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

void insert(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            current->children[ch] = new TrieNode();
        }
        current = current->children[ch];
    }
    current->isEndOfWord = true;
}

void printWords(TrieNode* root, std::string prefix) {
    if (root->isEndOfWord)
        std::cout << prefix << std::endl;

    for (auto& pair : root->children) {
        char ch = pair.first;
        TrieNode* childNode = pair.second;
        printWords(childNode, prefix + ch);
    }
}

int main() {
    TrieNode* root = new TrieNode();
    insert(root, "apple");
    insert(root, "banana");
    insert(root, "cherry");
    insert(root, "cat");
    printWords(root, "");
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 11

The 'printWords' function is called to print all the words in the TRIE. In this case, "apple," "banana," "cherry," and "cat" are inserted into the TRIE using the 'insert' function, so the output will be "apple, banana, cherry, cat".

Test: Tries - 2 - Question 12

What will be the output of the following code snippet?
#include <iostream>
#include <unordered_map>

struct TrieNode {
    std::unordered_map<char, TrieNode*> children;
    bool isEndOfWord;
};

void insert(TrieNode* root, std::string word) {
    TrieNode* current = root;
    for (char ch : word) {
        if (current->children.find(ch) == current->children.end()) {
            current->children[ch] = new TrieNode();
        }
        current = current->children[ch];
    }
    current->isEndOfWord = true;
}

int countPrefixes(TrieNode* root, std::string prefix) {
    int count = 0;
    TrieNode* current = root;
    for (char ch : prefix) {
        if (current->children.find(ch) == current->children.end())
            return 0;
        current = current->children[ch];
        count++;
    }
    return count;
}

int main() {
    TrieNode* root = new TrieNode();
    insert(root, "apple");
    insert(root, "banana");
    insert(root, "cherry");
    insert(root, "cat");
    std::cout << countPrefixes(root, "b") << std::endl;
    std::cout << countPrefixes(root, "ca") << std::endl;
    std::cout << countPrefixes(root, "apx") << std::endl;
    delete root;
    return 0;
}

Detailed Solution for Test: Tries - 2 - Question 12

The 'countPrefixes' function is called to count the number of prefixes in the TRIE. In this case, "apple," "banana," "cherry," and "cat" are inserted into the TRIE using the 'insert' function. The output will be 0 for "apx" because there is no word with that prefix in the TRIE, 1 for "ca" because "cat" has "ca" as a prefix, and 0 for "apx" because there is no word with that prefix in the TRIE.

Test: Tries - 2 - Question 13

Which of the following is NOT a step in the Boggle game using TRIE data structure?

Detailed Solution for Test: Tries - 2 - Question 13

Storing the frequency count of each word found in the TRIE is not a step in the Boggle game using TRIE data structure. The TRIE is primarily used for efficient word search and validation, but frequency counts are not directly related to the TRIE structure.

Test: Tries - 2 - Question 14

In the Boggle game, what is the purpose of a TRIE data structure?

Detailed Solution for Test: Tries - 2 - Question 14

The purpose of a TRIE data structure in the Boggle game is to store the dictionary of valid words. The TRIE allows for efficient word search and validation during the Boggle game.

Test: Tries - 2 - Question 15

Which of the following is NOT a typical optimization technique used in the Boggle game using TRIE data structure?

Detailed Solution for Test: Tries - 2 - Question 15

Heuristic algorithms are not typically used as optimization techniques in the Boggle game using a TRIE data structure. Optimization techniques such as memoization, backtracking, and pruning can be applied to improve the performance of the Boggle game. Heuristic algorithms, on the other hand, are not directly related to the TRIE structure or the game itself.

153 videos|115 docs|24 tests
Information about Test: Tries - 2 Page
In this test you can find the Exam questions for Test: Tries - 2 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Tries - 2, EduRev gives you an ample number of Online tests for practice

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF

Top Courses for Software Development