Programming interview questions and answers

Check if a linked list is a palindrome

Write a program to check if a linked list is a palindrome The approach:- Solution for this problem is recursion with a reference to head node of the list. Whenever the recursive function returns compare the node values and move the reference to the next node. C++ program to check if a linked list is …

Transpose a matrix

Write a program to transpose a matrix e.g.) Input matrix.1  2  34 5 67 8 9 Transposed matrix.1 4 72 5 83 6 9 C++ program to transpose a matrix #include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main() {srand(time(0));int m = 3;int n = 3;int** matrix = (int**) calloc(sizeof(int), m);for ( int i = 0; i …

Implement a minimum stack

Write a program to implement a stack that can return a minimum value of the current elements at any instance The Approach:- Have a second stack instance which will hold the minimum values. During a push operation if the new data is smaller than the top value in minimum stack push the data into the …

Compress a string

Write a program to compress a string. If input is “aabbb” expected output is “a2b3” The approach:- Have 2 pointers read and write to the input string. Scan the string using read pointer. If similar characters are seen keep incrementing a count. If character sequence changes, write the character and the count value at the …

Convert decimal number to binary string

Write a program to convert decimal number to binary string The approach:- Initialize a string array to hold the output binary string. mod the input number by 2.  If reminder is ‘1’ include ‘1’ in the output string. Else include ‘0’. Divide the number by 2 for next iteration. Repeat steps 2 and 3 till …

Convert decimal number to hexadecimal

Write a program to convert decimal number to hexadecimal The approach:- Initialize a string array to hold the output hexadecimal string. mod the input number by 16.  Map a hexadecimal digit based on the reminder value and append the output string. Divide the number by 16 for next iteration. Repeat steps 2 and 3 till …

Convert binary string to decimal number

Write a program to convert binary string to decimal number Parse the input string till end. On encountering a “1” compute power of 2 for the current position and add to a variable holding the result. C++ program to convert binary string to decimal number #include <iostream>#include <cstring>#include <cstdlib>#include <cmath>using namespace std;int bin2dec(char* str) {int …

Replace spaces in a string with "%20".

Write a program to replace spaces in a string with %20 Approach to replace spaces in a string with %20 Typically in this problem we are expected to use optimal memory. This requires that we extend the memory where we hold the input string. We start by counting the number of spaces in the input …

Find the longest palindrome in a string

Write a program to find the longest palindrome in a string The approach:- From each character location, compare the left and right locations for equality. Remember the location and maximum size of equality. Print the string starting from the location minus maximum size to location plus maximum size, which is the longest palindrome. C++ program …

Reverse words of a string using stack

Write a program to reverse words of a string using a stack The simplest approach to reverse words of a string is using a stack. To understand more about stack data structure refer C++ Stacks. Detect end of words and push into the stack. Then parse the contents of stack and print to get the string …