Shellbeehaken

1. Efficiently Search for Number "N" in a Million Integers

// Linear Search Use
#include <iostream>
#include <vector>
#include <algorithm>

bool searchNumber(const std::vector<int>& numbers, int N) {
    // Assume the numbers are not sorted; if they are sorted, use binary_search.
    return std::find(numbers.begin(), numbers.end(), N) != numbers.end();
}

int main() {
    std::vector<int> numbers = { /* a million integers */ };
    int N = 42;
    if (searchNumber(numbers, N)) {
        std::cout << "Number found.\n";
    } else {
        std::cout << "Number not found.\n";
    }
    return 0;
}

2. Sum of All Values in a Tree

#include <iostream>
#include <vector>

struct TreeNode {
    int value;
    std::vector<TreeNode*> children;
};

int sumTree(TreeNode* root) {
    if (root == nullptr) return 0;
    int sum = root->value;
    for (TreeNode* child : root->children) {
        sum += sumTree(child);
    }
    return sum;
}

int main() {
    TreeNode* root = new TreeNode{10, {new TreeNode{5, {}}, new TreeNode{3, {new TreeNode{2, {}}}}}};
    std::cout << "Sum of tree values: " << sumTree(root) << std::endl;
    return 0;
}

3. Print Numbers from N to 0 Using Recursion

void printNumbers(int N) {
    if (N < 0) return;
    std::cout << N << std::endl;
    printNumbers(N - 1);
}

4. Power of 2 Base Function without Using + or * (Using Bitwise Operations)

int powerOf2(int exponent) {
    return 1 << exponent;
}

int main() {
    int exponent = 3;
    std::cout << "2^" << exponent << " = " << powerOf2(exponent) << std::endl;
    return 0;
}

5. Distribute Candies Equally Among All Children (Pseudocode)

function distributeCandies(husband):
    totalChildren = 0
    for each wife in husband.wives:
        totalChildren += wife.children.count
    if totalChildren == 0:
        return "No children to distribute candies"
    candiesPerChild = husband.candies / totalChildren
    return candiesPerChild

6. Array Combinations to Match a Given Number

int countWays(const std::vector<int>& numbers, int target) {
    int n = numbers.size();
    std::vector<int> dp(target + 1, 0);
    dp[0] = 1; // There is one way to make 0, by selecting no numbers.

    for (int num : numbers) {
        for (int i = target; i >= num; --i) {
            dp[i] += dp[i - num];
        }
    }
    return dp[target];
}

int main() {
    std::vector<int> numbers = {2, 3, 7};
    int target = 7;
    std::cout << "Number of ways to sum up to " << 
    target << ": " << countWays(numbers, target) << std::endl;
    return 0;
}

7. Maximum Height of the Tree

#include <iostream>
#include <vector>

struct TreeNode {
    int value;
    std::vector<TreeNode*> children;
};

int maxHeight(TreeNode* root) {
    if (root == nullptr) return 0;
    int maxChildHeight = 0;
    for (TreeNode* child : root->children) {
        maxChildHeight = std::max(maxChildHeight, maxHeight(child));
    }
    return 1 + maxChildHeight;
}

int main() {
    TreeNode* root = new TreeNode{10, {new TreeNode{5, {}}, new TreeNode{3, {new TreeNode{2, {}}}}}};
    std::cout << "Maximum height of the tree: " << maxHeight(root) << std::endl;
    return 0;
}

8. What Happens When You Hit Enter After Writing the URL in the Search Bar?

When you hit enter after typing a URL:

  1. DNS Resolution: The browser contacts the DNS server to translate the domain name into an IP address.

  2. TCP Connection: The browser establishes a TCP connection with the server using the IP address (3-way handshake).

  3. HTTP/HTTPS Request: The browser sends an HTTP/HTTPS request to the server.

  4. Server Processing: The server processes the request and generates a response.

  5. HTTP/HTTPS Response: The server sends the response back to the browser.

  6. Rendering: The browser renders the content (HTML, CSS, JavaScript) and displays it to the user.

  7. Additional Requests: The browser may make additional requests for resources like images, stylesheets, and scripts.

9. Indexes in DBMS

Indexes are data structures that improve the speed of data retrieval operations on a database table. They are used to quickly locate data without having to search every row in a table. Indexes are typically created on columns that are frequently used in search conditions or join conditions.

10. Difference Between TRUNCATE and DROP

  • TRUNCATE: Removes all rows from a table without logging individual row deletions. It is faster and uses fewer system and transaction log resources. It does not remove the table structure or its dependencies.

  • DROP: Completely removes the table from the database along with its structure, data, indexes, and dependencies. It is a more destructive operation than TRUNCATE.

Last updated