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. |
Given a string as an input, write a program to reverse the words in the given string and display the new string. |
|
Answer» Example: We maintain a vector of strings. We traverse the given string and keep forming the word until a space is encountered. When space is encountered we ADD the word formed to the vector of strings. In the end, we print the vector of strings in reverse format. Code : #include <bits/stdc++.h>USING namespace std;// function to reverse the words in an inputted stringvoid displayModifiedString(string str){ // vector to store all the words of the inputted string vector<string> arr; string temp = ""; // VARIABLE to form each word of the string for (int i = 0; i < str.length(); i++) { // if space is encountered, we push the formed word to the vector if (str[i] == ' ') { arr.push_back(temp); temp = "";// RESETTING the temp variable } // we keep forming the word else temp += str[i]; } arr.push_back(temp);// we push back the last formed word // we print the vector in the reverse order for (int i = arr.size() - 1; i > 0; i--) cout << arr[i] << " "; cout << arr[0] << endl;} int main(){ string str = "InterviewBit is best"; displayModifiedString(str); return 0;}Output : best is InterviewBitExplanation : In the above code, the function displayModifiedString takes a string input and displays the modified string with words in reverse order. We keep forming a word until a space character is found. Once space is found, we push back the word to the vector. Lastly, we display the vector in reverse order. |
|
| 2. |
Given two unsorted arrays. Check if the second array is a subarray of the first array. |
|
Answer» Example: Input : We use two pointers to explore both the ARRAY and the subarray. We maintain the pointer of array arr2[] and increase the pointer of both ARRAYS if any element of arr1[] matches the first element of arr2[], otherwise set the pointer of arr1 to the next element of the previous starting point and reset the pointer of arr2 to 0. If all of arr2's elements match, print True; otherwise, print False. The following is how the above strategy is put into action: Code: #include <bits/stdc++.h>using namespace std; //function to check if the second array is a subarray of the first arraybool checkSubArray(int arr1[], int arr2[], int n, int m){ int i = 0, j = 0; while (i < n && j < m) { // If element matches // increment both pointers if (arr1[i] == arr2[j]) { i++; j++; // checking if we have REACHED the end of the second array if (j == m) return true; } else { i = i - j + 1;// setting the pointer of the first array to the next element of the previous starting point j = 0; // resetting the pointer of the second array to 0. } } return false;}int main(){ int arr1[] = { 2, 3, 0, 5, 1, 1, 2 }; int arr2[] = { 3, 0, 5, 1 }; int n = sizeof(arr1) / sizeof(arr1[0]); int m = sizeof(arr2) / sizeof(arr2[0]); bool res = checkSubArray(arr1, arr2, n, m); if (res) cout << "True\n"; else cout << "False\n"; return 0;}In the above code, the function checkSubArray checks whether the second array is a sub array of the first array or not and returns the output correspondingly. We maintain two pointers, one for the first array and the other for the second array. If the elements match, we increment both the pointers; otherwise, we reset the pointers. |
|
| 3. |
Differentiate between SQL and NoSQL databases. |
||||||||||||
|
Answer» Following are the differences between SQL and NoSQL databases:
|
|||||||||||||
| 4. |
Differentiate between preemptive and non-preemptive scheduling algorithms. |
||||||||||||||
|
Answer» Following are the differences between preemptive and non-preemptive scheduling algorithms:
|
|||||||||||||||
| 5. |
What are the advantages and disadvantages of subnetting? |
|
Answer» Following are the advantages of subnetting :
Following are the disadvantages of subnetting :
|
|
| 6. |
What do you understand by subnetting in the context of computer networks? |
|
Answer» Subnetting is the PROCESS of dividing a larger network into smaller networks in order to preserve security. As a result, smaller networks are easier to maintain. To divide a network into two pieces, select ONE bit from the host ID part for each Subnet. To divide a network into four subnets, two bits from the host id component must be CHOSEN for each subnet and so on. In the above image, we can SEE that we have divided the network 20.0.0.0/24 into two subnets 20.0.1.0/24 and 20.0.3.0/24. |
|
| 7. |
Write a query in SQL to find the details of an employee having the nth highest salary from a given table Employee. The value of n needs to be taken as user input. Assume that the table Employee has the following schema. |
|
Answer» name - denoting the name of the employee We USE the dense_rank() function to display the details of the employee having the nth highest salary. The function DENSE_RANK returns the rank of a row in an ordered collection of rows as a NUMBER. The ranks are in ascending order, starting with 1. Based on the values of the value exprs in the order by clause, DENSE RANK computes the rank of each row returned from a query in relation to the other rows as an analytic function. Query: select name, salary from(select name, salary, dense_rank() over(order by salary desc)input from Employee) where input = &n;Explanation: In the above query, we first sort the data according to the descending order of the salary and assign a rank to each of the EMPLOYEES starting from 1. In case of an EVENT where the salary of two employees is the same, they both are assigned the same rank. Then we display the data whose rank is equal to the given input. |
|
| 8. |
Differentiate between WHERE clause and HAVING clause in SQL. |
||||||||||||||||
|
Answer» The following table lists the differences between the WHERE CLAUSE and the HAVING clause:
|
|||||||||||||||||
| 9. |
Explain the Open Systems Interconnection (OSI) model in the context of computer networks. |
|
Answer» Following are the different layers in the OSI model: Physical Layer: The physical layer is the lowest layer in the OSI reference model. It is in charge of establishing a physical connection between the devices. BITS of information are stored in the physical layer. Following are the responsibilities of the physical layer :
Data Link Layer (DLL): The data link layer is in charge of message transport from node to node. The major purpose of this layer is to ensure that data transfers from one node to another through the physical layer are error-free. Following are the responsibilities of data link layer :
Network Layer: The network layer is responsible for data transmission between hosts on different networks. Following are the responsibilities of the network layer :
Transport Layer: The transport layer delivers services to the application layer while also receiving services from the network layer. Segments are the units of data in the transport layer. Following are the responsibilities of the Transport layer :
Session Layer: The Session Layer is in charge of establishing connections, maintaining sessions, authenticating users, and ensuring security. Following are the responsibilities of session layer :
Presentation Layer: The Presentation Layer is often referred to as the Translation Layer. The data from the application layer is retrieved and processed here so that it may be transmitted across the network in the proper format. Following are the responsibilities of the presentation layer :
Application Layer: The Application layer, which is implemented by network applications, is at the very top of the OSI Reference Model stack of levels. Following are the responsibilities of the application layer :
|
|
| 10. |
Differentiate between new and malloc() in the context of C++. |
||||||||||||
|
Answer» The following table lists the differences between new and malloc():
|
|||||||||||||
| 11. |
Differentiate between Delete and Truncate SQL commands. |
||||||||||||||||||||
|
Answer» The following table lists the differences between Delete and Truncate SQL commands:
|
|||||||||||||||||||||
| 12. |
What are the different types of SQL commands? |
|
Answer» Following are the different types of SQL commands :
|
|
| 13. |
What do you understand by super key, candidate key, primary key and foreign key in the context of database management systems? |
Answer»
For example, let US consider tables with the following schema : Teacher : teacher_id (primary key), teacher_name, teacher_phone_number, teacher_aadhar, teacher_department_idDepartment : department_id (primary key), department_nameHere, for the Teacher table, the following are the DIFFERENT types of keys present :
|
|
| 14. |
You have 15 rupees on you. You enter a shop and the shopkeeper informs you that each chocolate costs one rupee. He also informs you that in exchange for three wrappers, you will receive a chocolate. How many chocolates can you eat in total? |
|
Answer» We can eat a total of 22 CHOCOLATES for Rs. 15. First, we will buy 15 chocolates for 15 rupees since each chocolate costs Re. 1. Now, we have 15 wrappers. We return all the 15 wrappers to the SHOPKEEPER which gives us 5 chocolates (15 / 3 = 5). We eat those five chocolates and return 3 wrappers to the shopkeeper and GET 1 chocolate. We eat that chocolate and again GIVE 3 wrappers which give us ONE chocolate. So, in total, we can have 15 + 5 + 1 + 1 chocolates. |
|
| 15. |
Write a program to sort a given array of numbers. The sorting algorithm should give best performance in every case (best, worst and average). Example : Input : arr = {3, 5, 7, 1, 2, 4, 6} Output : {1, 2, 3, 4, 5, 6, 7} |
|
Answer» Example: Since we want the sorting algorithm to give the best time complexity in all three cases (that is, best, worst and average), we will implement merge sort to sort the given array of numbers. In merge sort, we divide an array into two HALVES recursively, sort each half and then merge them. Code: #include<bits/stdc++.h>using namespace STD;//function to merge the two sorted halves of the arrayvoid merge(int *arr, int start, int mid, int end) { int temp[end - start + 1];// creating a temporary array to store the sorted array int i = start; int j = mid+1; int k = 0; while(i <= mid && j <= end) { if(arr[i] <= arr[j]) { temp[k] = arr[i]; i = i + 1; } else { temp[k] = arr[j]; j = j + 1; } k = k + 1; } //If the first half still has elements, we add them to the temporary array while(i <= mid) { temp[k ++] = arr[i ++]; } //If the second half still has elements, we add them to the temporary array while(j <= end) { temp[k ++] = arr[j ++]; } // We store the sorted order of elements in the original array for(i = start; i <= end; i ++) { arr[i] = temp[i - start] }}// function to sort an array of elementsvoid mergeSort(int *arr, int start, int end) { if(start < end) { int mid = (start + end) / 2;// finding the mid INDEX mergeSort(arr, start, mid);// sorting the left side of the array mergeSort(arr, mid+1, end);// sorting the right side of the array merge(arr, start, mid, end);// merging the left and right halves of the array }}int main(){ int arr[] = {3, 5, 7, 1, 2, 4, 6}; int n = sizeof(arr)/ sizeof(arr[0]); mergeSort(arr, 0, n); cout << "The sorted array is : "; for(int i = 0; i < n; i ++) cout << arr[i]; cout << "\n"; return 0;}Output: The sorted array is : 1 2 3 4 5 6 7Explanation: In the above code, the function merge() merges the two sorted halves of the array. The function mergeSort() recursively breaks the problem into subproblems, sorts each half and then merges the sorted halves by calling the merge() function. |
|
| 16. |
Write a program to calculate the Least Common Multiple (LCM) of two numbers. Example : Input : a = 10, b =15 Output : 30 a = 5, b = 7 Output : 35 |
|
Answer» Input : Let us assume the two numbers are a and b. Then, the two numbers have the following relationship : a * b = LCM(a, b) * GCD(a, b)or, LCM(a, b) = (a * b) / GCD(a, b)Let us take an example to UNDERSTAND better. For a = 10 and b = 15, a * b = 150, LCM(10, 15) = 30, GCD(10, 15) = 5. So, a * b = LCM(a, b) * GCD(a, b). Code : #include <iostream>using namespace std; //Function to find the GREATEST common divisor of the two numbersint findGCD(int a,int b){ if (b == 0) return a; return findGCD(b, a % b);} // Function to return LCM of two numbersint findLCM(int a, int b){ return (a * b) / findGCD(a, b);} int MAIN(){ int a = 10, b = 15; cout <<"The Least Common Multiple of the two numbers " << a << " and " << b << " is : " << findLCM(a, b); return 0;}Output: The Least Common Multiple of the two numbers 10 and 15 is : 30Explanation: In the above code, the function findGCD() finds the greatest common divisor of the two numbers a and b. We use the Euclidean algorithm to find the greatest common divisor of the two numbers. The function findLCM() finds the least common multiple of the two numbers. |
|
| 17. |
Does Java allow a class to inherit from multiple classes? If not, how can a class achieve the properties of more than one class (i.e., multiple inheritance)? |
|
Answer» Multiple inheritances are not allowed by Java using classes. This is because dealing with the complexity that multiple inheritances PRODUCE is quite difficult. For example, import java.io.*;class Base1 { void fun() { System.out.println("Parent1"); }} class Base2 { void fun() { System.out.println("Parent2"); }} class Child extends Base1, Base2 { public static void main(String ARGS[]) { Child obj = new Child(); // object creation of the child class obj.fun(); // it leads to compilation error since fun() is defined in both Base1 and Base2 and the compiler gets in an ambiguous situation as to which fun() is being referred to. }}In the above code, a compilation error is thrown. This is because both the classes Base1 and Base2 contain a definition for the function fun(). This confuses the compiler as to which fun() is being referred to. Multiple Inheritance causes issues during operations such as casting, constructor chaining, and so on, and the main reason is that we only NEED multiple inheritances in a few cases, thus it's best to leave it out to keep things simple and easy. However, the main objective of multiple inheritances to inherit from multiple classes can be obtained via interfaces in Java. An interface, LIKE a class, has variables and methods, however, unlike a class, the methods in an interface are abstract by default. The class that implements an interface needs to define the member functions. If a class implements multiple interfaces, or if an interface extends multiple interfaces, multiple inheritances via interface happen. For example, the code for the above example using interfaces would be like this: interface Base1{ public void fun();}interface Base2{ public void fun();}class Child implements Interface1, Interface2{ public void fun() { System.out.println("Implementing the fun() of the interface."); } public static void main(String args[]){ Child obj = new Child(); obj.fun(); }}In the above code, the fun() is not defined in the interfaces Base1 and Base2. They are defined by the classes which implement the interfaces. This leads to no ambiguity and the purpose of multiple inheritances has been solved. |
|
| 18. |
Differentiate between interface and abstract class in the context of Java. |
||||||||||||||||||
|
Answer» The following table lists the differences between an interface and abstract class:
|
|||||||||||||||||||
| 19. |
What are the advantages of Object Oriented Programming? |
|
Answer» Following are the advantages of Object-Oriented Programming:
|
|
| 20. |
What do you understand about Object Oriented Programming? Explain the major features of object oriented programming. |
|
Answer» Object-oriented programming (OOP) is a programming paradigm that organises software design around data, rather than functions and logic. An object is a data field with its own set of properties and behaviour. Object-oriented programming (OOP) focuses on the objects that developers desire to handle rather than the logic that is required to manipulate them. This kind of programming is ideally suited to big, complicated, and frequently updated or maintained projects. Inheritance, hiding, polymorphism, and other real-world concepts are all part of object-oriented programming. The basic goal of OOP is to connect data and the functions that operate on it so that no other part of the code may access it except that function. Following are the major FEATURES of Object-Oriented Programming:
|
|
| 21. |
Differentiate between Multiprogramming vs Multitasking. |
||||||||||||
|
Answer» Following are the differences between Multiprogramming and Multitasking:
|
|||||||||||||
| 22. |
Explain HTTP and HTTPS protocol in the context of computer networks. |
Answer»
|
|