InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Determine whether there is a triplet in the array whose total equals the given value, given an array and a value. If a triplet of this type exists in the array, print it and return true. If not, return false. |
|
Answer» Example: Input: arr = {12, 3, 4, 1, 6, 9}, sum = 24;Output: 12, 3, 9Explanation: The array contains a triplet (12, 3 and 9) whose sum is 24. Input: arr = {1, 2, 3, 4, 5}, sum = 9Output: 5, 3, 1Explanation: There is a triplet (5, 3 and 1) present in the array whose sum is 9. Approach: The algorithm's performance can be increased by sorting the array. The two-pointer technique is USED in this effective method. Fix the first element of the triplet by traversing the array. Find if there is a pair whose TOTAL equals x – array[i] using the Two Pointers technique. Because the two pointers approach takes linear time, it is preferable to a nested loop. Code: #include <bits/stdc++.h>using namespace std;// returns true if there is triplet with sum equal// to 'sum' present in arr[]. Also, prints the tripletbool findTriplet(int arr[], int arr_size, int sum){ int low, high; //sorting the entire array sort(arr, arr + arr_size); //Fixing the first element one by one and finding the other two elements for (int i = 0; i < arr_size - 2; i++) { // Start two index variables from opposite corners of the array and move them toward each other to find the other two MEMBERS. low = i + 1; // index of the first element in the remaining elements high = arr_size - 1; // index of the last element while (low < high) { if (arr[i] + arr[low] + arr[high] == sum) { printf("The triplet is %d, %d, %d", arr[i], arr[low], arr[high]); return true; } else if (arr[i] + arr[low] + arr[high] < sum) low++; else // arr[i] + arr[low] + arr[high] > sum high--; } } // If we reach here, then no triplet was found return FALSE;}int MAIN(){ int arr[] = { 12, 3, 4, 1, 6, 9 }; int sum = 24; int arr_size = sizeof(arr) / sizeof(arr[0]); findTriplet(arr, arr_size, sum); return 0;}Output: The triplet is 12, 3, 9Explanation: In the above code, the function findTriplet takes the input of an array, the size of the array and the sum required. First of all, we sort the array given. We traverse the array elements and fix the first element of the triplet in each iteration. We use a two-pointer approach and find the remaining two elements of the triplet, if it is possible with the given array. Useful Resources:
|
|
| 2. |
Write a program to print the factorial of a given input number. |
|
Answer» Example: Input: 5120In practically all interviews, factorial is one of the most frequently asked questions (INCLUDING developer interviews) Developer interviews place a greater emphasis on programming concepts such as dynamic programming, recursion, and so on, whereas Software Development Engineer in TEST interviews place a greater emphasis on handling edge scenarios such as max values, min values, negative values, and so on, and approach/efficiency becomes secondary. The following recursive formula can be used to calculate factorial. n! = n * (n-1)!n! = 1 if n = 0 or n = 1Code: #include <bits/stdc++.h>using namespace std;// function to find the factorial of given numberint findFactorial(unsigned int n){ if (n == 0) return 1; return n * factorial(n - 1);}int main(){ int number = 5; if(number < 0) { cout << "Negative numbers do not have factorial" << "\n"; return 0; } cout << "Factorial of " << number << " is " << findFactorial(number) << “\n”; return 0;}Output: Factorial of 5 is 120Explanation: |
|
| 3. |
Write a programme to check whether the pairs and ordering of "", "", "(", ")", "[", "]" in the expression string expression are balanced or not. |
|
Answer» By balanced string, it is meant that every opening parenthesis should have its corresponding closing braces and there must not be redundant closing braces. Example : INPUT : “[()]{}{[()()]()}”OUTPUT : BalancedInput: [[Output: Not BalancedApproach: We create a character stack and now look through the expression string. If the current character is a beginning bracket ('(', ", or '['), stack it. If the current character is a closing bracket (') or " or ']'), pop from the stack; if the popped character is the matching starting bracket, all is well; otherwise, the BRACKETS are unbalanced. If some starting brackets remain in the stack after traversal, the stack is said to be "unbalanced." Code: #include <bits/stdc++.h>using namespace STD;// function to check if the given expression is balanced or notbool checkBalancedExpression(string str){ stack<char> char_stack; char top_element; // Traversing the Expression for (int i = 0; i < str.length(); i++) { if (str[i] == '(' || str[i] == '[' || str[i] == '{') { // We push the character in the stack char_stack.push(str[i]); continue; } // We check if the stack is empty. If it is empty, then it is an unbalanced expression since there must be a corresponding opening bracket before the closing one. if (char_stack.empty()) return false; switch (str[i]) { case ')': top_element = char_stack.top(); char_stack.pop(); if (top_element == '{' || top_element == '[') return false; break; case '}': top_element = char_stack.top(); char_stack.pop(); if (top_element == '(' || top_element == '[') return false; break; case ']': top_element = char_stack.top(); char_stack.pop(); if (top_element == '(' || top_element == '{') return false; break; } } // The stack should be empty at this point for a balanced expression return (char_stack.empty());}int main(){ string str = "[]{}()"; if (checkBalancedExpression(str)) cout << "Balanced"; else cout << "Not Balanced"; return 0;}Output : BalancedExplanation: In the above code, we create a function checkBalancedExpression which takes the input of an expression string and CHECKS whether the string is balanced or not. We start traversing the expression. Whenever we encounter an opening bracket, we push it in the stack. In the case of a closing bracket, we first check if the stack is empty or not. If it is empty, we directly return false otherwise, we check if the top element has the corresponding opening bracket or not. |
|
| 4. |
Write a program that reverses a given input number. |
|
Answer» EXAMPLE : Input: 1234Output: 4321Approach: We follow a greedy approach. We start extracting each DIGIT from the original number from the end and keep on ADDING it at the end of the new number, thereby reversing the given number. Code: #include <bits/stdc++.h>using namespace STD;//function to find the reverse of the given inputint reverseNumber(int original_number){ int new_number = 0; while (original_number > 0) { new_number = new_number * 10 + original_number % 10; // extracting the last digit from the original number and appending it to the new number original_number = original_number / 10; } return new_number;}int main(){ int original_number = 1234; cout << "The original number is " << original_number << "\n"; cout << "The reversed number is " << reverseNumber(original_number) << "\n"; return 0;}Output : The original number is 1234The reversed number is 4321Explanation : In the above code, the function reverseNumber TAKES the input of an integer number and returns the reverse of the number. We extract each digit from the end and append it to the new number. |
|
| 5. |
Swap the values of two variables, x and y, without needing a third variable. |
Answer»
The goal is to find a sum in one of the two numbers provided. The total and subtraction from the sum can then be used to SWAP the INTEGERS. Code: #include <bits/stdc++.h>using namespace std;int main(){ int a = 1, b = 2; cout << "Before Swapping : a = " << a << " and b = " << b << "\n"; a = a + b; // storing the sum of a and b in a b = a - b; // storing the value of the original a in b a = a - b; // storing the value of the original b in a cout << "After Swapping : a = " << a << " and b = " << b << "\n";}OUTPUT : Before Swapping : a = 1 and b = 2After Swapping : a = 2 and b = 1Explanation :
To swap two variables, use the bitwise XOR operator. When two integers x and y are XORed, the result is a number with all bits set to 1 wherever the bits of x and y differ. For instance, the XOR of 10 (in Binary 1010) and 5 (in Binary 0101) is 1111, while the XOR of 7 (0111) and 5 (0101) is 1111. (0010). We can then xor the resultant XORed with the other number to swap the values. Considering the above example, when we xor 1111 with 0101 we get 1010. Code : #include <bits/stdc++.h>using namespace std;int main(){ int a = 1, b = 2; cout << "Before Swapping : a = " << a << " and b = " << b << "\n"; a = a ^ b; // storing the xor of a and b in a b = a ^ b; // storing the value of the original a in b a = a ^ b; // storing the value of the original b in a cout << "After Swapping : a = " << a << " and b = " << b << "\n";}Output : Before Swapping : a = 1 and b = 2After Swapping : a = 2 and b = 1Explanation : In the above code, we first stored the xor of both the numbers in the first variable. Then, we store the original value of the first variable in the second variable by XORing the second variable with the sum. Similarly, we change the value for the second variable as well. Thus, we swapped the two numbers without using a third variable. |
|