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. |
SQL Query to find min, max and average salary from a table? |
||||||||
|
Answer» Suppose ASSUME the table is in the format shown below,: Employee table:
To FIND the maximum salary, SELECT MAX(Salary) from Employee; // To just get the max salarySELECT * from Employee where Salary = (SELECT MAX(Salary) from Employee); // Employees with highest salaryTo find the average salary, SELECT AVG(Salary) from Employee;To find the MINIMUM salary, SELECT MIN(Salary) from Employee; // To just get the min salarySELECT * from Employee where Salary = (SELECT MIN(Salary) from Employee); // Employees with lowest salaryUseful Interview Resources
|
|||||||||
| 2. |
Difference between List and Tuple in Python? |
Answer»
|
|
| 3. |
Advantages of a vector over an array in C++? |
Answer»
|
|
| 4. |
What is the size of empty class in C++? |
|
Answer» #include <iostream>using namespace STD;class MyClass{};int main() { COUT<<sizeof(MyClass)<<"\n"; }// Output:// 1 The size of an empty class in C++ is 1 byte. The reason BEHIND this is to make SURE that 2 different objects have 2 different addresses. If they have the same addresses then there is no way to differentiate whether two objects are the same or different. It all boils down to the identity of the object. |
|
| 5. |
Write a python program to filter list elements between 1 to 20 (both inclusive) which are even numbers? |
|
Answer» This can be done in many WAYS. But the more pythonic WAY of doing this is to use filter and lambda. filter() method takes a function and a sequence. The function should return true or false. It runs a function on each element in the sequence and returns an iterator for the elements for which the function returns true. We can pass a normal function to filter but the more pythonic way of doing it is to pass a lambda. nums = list(range(1, 21)) # Include 20filtered_elements = list(filter(lambda x: (x%2 == 0), nums)) # CONVERT iterator to list |
|
| 6. |
What will be the output of the below python program? |
|
Answer» class MYCLASS: def __init__(self): self.x=10 self.__y=20 obj=myClass()print(obj.__y) Private name mangling: When an identifier that textually occurs in a class definition begins with TWO or more underscore characters and does not END in two or more UNDERSCORES, it is considered a private name of that class. Private names are transformed to a longer form before code is generated for them. The transformation inserts the class name in FRONT of the name, with leading underscores removed, and a single underscore inserted in front of the class name. For example, the identifier __spam occurring in a class named Ham will be transformed to _Ham__spam. Hence the above example throws an error 'AttributeError: myClass object has no attribute __y' because the name is transformed into _myClass__y. So if we instead print(obj._myClass__y), this will work and prints 20. |
|
| 7. |
There are 9 coins and a weighing balance. 8 coins are of the same weight and 1 coin is heavier. In how many minimum numbers of iterations can you find out the heaviest coin in the worst case? |
|
Answer» Most people try to divide the coins into 2 groups. But the TRICK here is to divide them into 3 groups. The reason being if we divide coins into 2 groups and weigh them, we can only discard one group. But if we divide them into 3 groups and weigh 2 groups, either they both will be equal or not. If both are equal then the coin with heavier weight will be on the LEFT out group and if they both are not equal then the heavier weight group contains the coin with heavier weight. We can discard 2 groups out of the 3 groups in this case. Hence we first divide 9 coins into 3 equal groups.
|
|
| 8. |
What is Bootstrap? What are the advantages of Bootstrap over CSS? |
|
Answer» BOOTSTRAP is an Open Source Front-End framework developed by Twitter that is used for making web development easier and faster. This is NOTHING but a reusable code that we can freely download and use for our own web development so that we get the functionality without having to re-write the same code (i.e., not inventing the wheel again). It supports ALMOST all of the browsers and hence can WORK with any browser. Bootstrap also makes the website responsive i.e., they fit the screen no matter which device is being used to view the website. Advantages of Bootstrap over CSS:
|
|
| 9. |
Print 1-100 without using loops? |
|
Answer» To print 1-100 without LOOP, we can leverage RECURSION for this. #include<iostream>void printVal(int i){ if(i > 100) return; STD::cout<<i<<"\n"; printVal(++i);}int MAIN() { int i = 1; printVal(i);} |
|
| 10. |
What is the output of the following code: |
|
Answer» INT main() { for(;;) std::cout<<"hello\n";} Let us see what for does. for loop does 3 things, initialization, condition check and expression to evaluate before running the next iteration. These 3 things are separated by a semicolon (;). Whatever logic is needed inside for loop is placed inside { } parenthesis. If we have a SINGLE line body then this single line can follow for loop without having the { } parenthesis. One more thing to note is that when the condition check is empty, it is replaced by a non-zero constant by the compiler. for(;;) // This is fine because condition is replaced by nonzero-constant by compiler as per the specification of C++ languagewhile() // This gives an error as it doesn't have condition expressionif() // This also gives an error as it doesn't have condition expressionHere, in this case, the initialization is empty, condition check is empty and the expression to evaluate before running the next iteration is also empty and has a single line body of the print statement. As the condition is replaced by a non-zero constant by the compiler, it is always evaluated to true and hence the loop runs on forever. So an INFINITE loop printing Hello on the SCREEN. |
|
| 11. |
What is a dangling pointer? How to handle it? |
|
Answer» A dangling pointer is a pointer pointing to a location that was already FREED. De-referencing a dangling pointer causes undefined behaviour. For example: struct MyStruct{ int myInt; char myChar;};int main(){ MyStruct* firstPtr = NEW MyStruct(); // .... some code here MyStruct* secondPtr = firstPtr; secondPtr->myInt = 5; secondPtr->myChar = 'A'; DELETE secondPtr; // ... some code here std::cout<<firstPtr->myInt<<" "<<firstPtr->myChar<<"\n";}In the above example, the secondPtr which points to the same address the firstPtr is pointing to gets deleted and hence later when firstPtr is dereferenced, it gives undefined behaviour. Here the firstPtr becomes a dangling pointer as it is still pointing to the MEMORY that got deleted. To avoid having dangling POINTERS, we can use smart pointers (or) check all the pointers that are being referenced and explicitly make them NULL so that they don't point to the deleted memory. |
|
| 12. |
What is volatile in C? |
|
Answer» Volatile is a keyword in C that tells the compiler not to optimize anything that is related to the volatile variable and always fetch the value of the variable from the memory for every use of the volatile variable. For example, int RUN = 1;while(run){ // Do something and don't involve run variable at all}In this case, the compiler sees that the variable run is not being used at all in the loop and hence can optimize the while loop into while(1). But run can be changed by a signal handler or OS etc. If we make the variable run as volatile, then the compiler doesn't do any such optimization. The 3 major places where volatile is used (i.e., variable can change without action from visible CODE) is: |
|
| 13. |
Check if a string is a palindrome or not? |
|
Answer» Palindrome: A string is called a palindrome if it can be read the same way starting from the front (or) starting from the back. In other words, a string is a palindrome if the reverse of the string is equal to the original string. Naive Implementation: #include<iostream>#include<string>#include<algorithm>using namespace std;// To reverse a stringstring reverseString(string str){ int len = str.length(); int mid = len / 2; // Traverse until mid and swap characters from ends for (int i = 0; i < mid; i++) swap(str[i], str[len - i - 1]); return str;}BOOL isPalindrome(string a){ // This reverse the string string b = reverseString(a); // Check if REVERSED string and original string are same return a == b;}int main(){ string s1 = "BANANA"; string s2 = "MADAM"; cout<<s1<<" is "<< (isPalindrome(s1) ? "NOT ": "") <<" a PALINDROME\n"; cout<<s2<<" is "<< (isPalindrome(s2) ? "NOT ": "") <<" a PALINDROME\n";}Output: BANANA is NOT a PALINDROMEMADAM is a PALINDROMEThis approach has a Time Complexity of O(N) and ALSO Space Complexity of O(N). A better version to solve this is to not create the actual copy but use 2 pointers one from start and another from the end to check if these 2 pointers POINT to the same characters and move them until the midpoint of the string. #include<iostream>#include<string>#include<algorithm>using namespace std;bool isPalindrome(string a){ int len = a.length(); int start = 0; int end = len-1; while(start < end) { // if not same then return false if(a[start] != a[end]) { return false; } // update the start and end start++; end--; } // If it had reached upto this, then all characters are equal until mid return true;}int main(){ string s1 = "BANANA"; string s2 = "MADAM"; cout<<s1<<" is "<< (isPalindrome(s1) ? "NOT ": "") <<" a PALINDROME\n"; cout<<s2<<" is "<< (isPalindrome(s2) ? "NOT ": "") <<" a PALINDROME\n";}Output: BANANA is NOT a PALINDROMEMADAM is a PALINDROMEThis approach has a Time Complexity of O(N). Space Complexity is O(1) as we are not making a copy of the string here. |
|
| 14. |
What is merge sort? What is the Time & Space Complexity for merge sort? |
|
Answer» Merge Sort is a Divide & Conquer algorithm that is used to sort a given INPUT (can be an ARRAY or linked list). It involved dividing the input into 2 halves at each step until input has only 1 element and then merging these sorted halves to finally get a sorted input. Merge sort is not an in-place algorithm (which means you NEED to use extra SPACE for sorting the input).
|
|
| 15. |
Difference between linear and non-linear data structures? |
||||||||||||||
|
Answer» Data STRUCTURES are divided into 2 types based on the shape/ based on the arrangement of the data elements. They are Linear Data Structures and Non-Linear Data Structures.
|
|||||||||||||||
| 16. |
Difference between C & C++? |
||||||||||||||
Answer»
|
|||||||||||||||
| 17. |
What is the difference between Binary Tree and Binary Search Tree? |
Answer»
|
|