Problem Statement:
The United Nations Organization released an official document regarding the most important events from the beginning of time (dated 00-00-0000) with a brief description of the events. The date of all the events is mentioned in the 'DD-MM-YYYY' format.
Find the total number of distinct years referenced in the document.
Input Specification:
Output Specification:
Example 1:
Input: input1: UN was established on 24-10-1945. India got freedom on 15-08-1947.
Output: 2
Explanation:
2 distinct years, 1945 and 1947 have been referenced.
Problem Statement:
An array has a special integer if that integer can be formed by calculating the sum of a number and its reverse.
Find how many integers are special in an array.
Note: Trailing zeros from the reverse of a number formed need to be removed if they exist.
Input Specification:
Output Specification:
Example 1:
Input:
input1: {22, 121}
input2: 2
Output:
2
Problem Statement:
Write a MySQL query to find:
The count (use alias count) of express trains that start from New Delhi (NDLS) and go to Mumbai (BCT) or Bhopal (BHP).
Your output should have 1 column as given below:
Topics Involved / Prerequisites
Overview To extract the distinct years from a continuous text string, Regular Expressions (Regex) are the cleanest and most robust approach. We can define a pattern that specifically looks for the standard date format and captures only the year portion. Storing these captured years in a Hash Set automatically filters out any duplicates.
Approach
1. Pattern Definition We define a regex pattern \b\d{2}-\d{2}-(\d{4})\b. This strictly looks for a word boundary, two digits, a hyphen, two digits, a hyphen, and exactly four digits. The parentheses around the four digits create a "capture group" so we can easily extract just the year.
2. String Iteration Using C++'s sregex_iterator, we scan through the entire document string. Every time the iterator finds a match matching our date format, we extract the first capture group (the year).
3. Uniqueness Filter We insert each extracted year into a std::unordered_set. Since sets cannot contain duplicate values, it naturally handles cases where the same year is mentioned multiple times. Finally, we return the size of the set.
Code Implementation (C++)
#include <iostream>
#include <string>
#include <regex>
#include <unordered_set>
using namespace std;
int countDistinctYears(string input1) {
// Regex pattern to match DD-MM-YYYY and capture the 4-digit year
regex pattern("\\b\\d{2}-\\d{2}-(\\d{4})\\b");
sregex_iterator it(input1.begin(), input1.end(), pattern);
sregex_iterator end;
unordered_set<string> distinct_years;
while (it != end) {
distinct_years.insert((*it)[1].str());
it++;
}
return distinct_years.size();
}Time and Space Complexity
Space Complexity: O(K) — Where K is the number of distinct years stored in the set.
Topics Involved / Prerequisites
Overview
For a given target number N, we need to determine if there exists an integer X such that X + reverse(X) = N.
By converting the provided strings into integers, we can systematically check possible values of X to see if they satisfy the condition for each element in the array.
Approach
1. Number Reversal Helper
We create a helper function that reverses an integer mathematically (using modulo 10 to extract digits and multiplying by 10 to shift them). This inherently handles the rule about dropping trailing zeros (e.g., reversing 120 yields 21).
2. Target Evaluation
For each string in the input array, we convert it to a 64-bit integer (long long) to safely handle large values.
3. Iterative Checking
For a specific target N, we loop an integer X from 1 up to N. If X + \text{reverse}(X) == N, we mark the number as special, increment our global counter, and immediately break the inner loop to move on to the next element in the array.
C++ Implementation
#include <iostream>
#include <vector>
#include <string>
using namespace std;
long long reverseNumber(long long n) {
long long rev = 0;
while (n > 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
return rev;
}
int countSpecialIntegers(vector<string> input1, int input2) {
int special_count = 0;
for (int i = 0; i < input2; i++) {
// Convert string to long long to handle larger numbers safely
long long target = stoll(input1[i]);
bool is_special = false;
// Check all possible numbers up to the target
for (long long x = 1; x <= target; x++) {
if (x + reverseNumber(x) == target) {
is_special = true;
break; // Found a match, no need to keep checking this target
}
}
if (is_special) {
special_count++;
}
}
return special_count;
}Time and Space Complexity
Topics Involved / Prerequisites
Overview To retrieve this specific count, we need to apply multiple filtering conditions to a hypothetical trains table. We use the WHERE clause to narrow down the dataset and an aggregate function to count the valid rows.
Approach
1. Aggregation and Aliasing We use SELECT COUNT(*) to count the number of rows that match our criteria. We immediately follow it with AS count to strictly meet the problem's requirement of having exactly 1 column named "count".
2. Applying Conditions In the WHERE clause, we use AND to combine the strict requirements:
3. Handling Multiple Destinations Because the destination can be one of two options, we use the IN operator (destination IN ('BCT', 'BHP')), which is cleaner and more optimal than writing multiple OR statements.
(Note: Table names liketrainsand column names liketrain_type,source, anddestinationare assumed standard identifiers. You may need to map them to the exact schema provided in your specific testing platform).
SQL Query
SELECT COUNT(*) AS count
FROM trains
WHERE train_type = 'express'
AND source = 'NDLS'
AND destination IN ('BCT', 'BHP');