
Most of the service-based and product-based companies include coding-related questions in their interview questions. Given here in this article are some of the top interview questions asked in the technical interview.
| Array in Data Structures |
| # Creating an array my_array = [1, 2, 3, 4, 5] (integer type elements stored in my_array.) # Accessing elements first_element = my_array[0] #Output 1 second_element = my_array[1] # Output 2 |
| Kadane’s algorithm |
|
| Reverse in String |
| string reverseWords(string S) { string temp=""; string ans=""; for(int i=S.length()-1;i>=0;i--){ if(S[i]=='.'){ reverse(temp.begin(),temp.end()); ans=ans+temp; ans.push_back('.'); temp=""; } else{ temp.push_back(S[i]); } } reverse(temp.begin(),temp.end()); ans=ans+temp; return ans; } |
| Longest Palindrome in a String |
| string longestPalin (string S) { // code here int n=S.size(); int start=0,end=0,maxl=1; //odd length for(int i=0;i<n;i++) { int l=i,r=i; while(l>=0 && r<n) { if(S[l]!=S[r]) { break; } l--; r++; } int len=r-l-1; if(len>maxl) { maxl=len; start=l+1; end=r-1; } } //even length for(int i=0;i<n;i++) { int l=i,r=i+1; while(l>=0 && r<n) { if(S[l]!=S[r]) { break; } l--; r++; } int len=r-l-1; if(len>maxl) { maxl=len; start=l+1; end=r-1; } } return S.substr(start,maxl); } |
| Competitive coding questions | |
| Linear data structure | Non-linear data structure |
| It is a structure in which data elements are adjacent to each other | It is a structure in which each data element can connect to over two adjacent data elements |
| Examples of linear data structure include linked lists, arrays, queues, and stacks | Examples of nonlinear data structure include graphs and trees |
| Difference between Stack and Array | |
| A stack is a linear data structure which follows LIFO (Last In, First Out) | An array is a linear data structure that collects the data as index. |
| The element can be accessed from the top of stack. | The element can be accessed from anywhere using the index. |
| The primary operation on stack are push, pop, peek and others. | In array we can read, write, modify elements at any specific index inside array. |
| Stack can be implemented using array, linked list and other data structures. | Array is a fundamental data structure and can help to implement other data structures. |
| Competitive coding Questions |
| if (str.equals(reverse)) { System.out.println("Palindrome"); } else { System.out.println("Not Palindrome"); } |
| Selection Sort Algorithm |
| #include <stdio.h> void selection_sort(int arr[], int n) { int i, j, min_index; // Traverse through all array elements for (i = 0; i < n – 1; i++) { // Find the minimum element in the unsorted part of the array min_index = i; for (j = i + 1; j < n; j++) { if (arr[j] < arr[min_index]) { min_index = j; } } // Swap the found minimum element with the first element int temp = arr[i]; arr[i] = arr[min_index]; arr[min_index] = temp; } } int main() { int my_array[] = {64, 25, 12, 22, 11}; int n = sizeof(my_array) / sizeof(my_array[0]); // Perform selection sort selection_sort(my_array, n); // Print the sorted array printf(“Sorted array: “); for (int i = 0; i < n; i++) { printf(“%d “, my_array[i]); } printf(“\n”); return 0; } |
| Quick sort algorithm |
| #include <bits/stdc++.h> using namespace std; // A utility function to swap two elements void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; } int partition (int arr[], int l, int h) { int pivot = arr[h]; // pivot int i = (l - 1); // Index of smaller element and indicates the right position of pivot found so far for (int k = l; k <= h - 1; k++) { // When the actual element is less than the pivot if (arr[k] < pivot) { i++; // increment index of smaller element swap(&arr[i], &arr[k]); } } swap(&arr[i + 1], &arr[h]); return (i + 1); } //A function to implement quicksort void quickSort(int arr[], int l, int h) { if (l < h) { //pi is a partitioning index, and //arr[p] is now in the correct location. int pi = partition(arr, l, h); // Separately sort elements before // partition and after partition quickSort(arr, l, pi - 1); quickSort(arr, pi + 1, h); } } /* Function to print an array */ void print_array(int arr[], int size) { int i; for (i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } int main() { int arr[] = {11, 13, 16, 1, 3, 5, 9}; int n = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, n - 1); cout << "Sorted array: \n"; printArray(arr, n); return 0; } |
| Reverse an Array |
| for (int t = 0; t < a.length / 2; t++) { int tmp = a[t]; a[t] = a[a.length - t - 1]; a[a.length - t- 1] = tmp; } |