You are on page 1of 7

1) Predict the output of the following snippets.

a) #include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **p);
}
b) #include <stdio.h>
void main()
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d%d%d\n", k, *p, **m);
}
Options:
a) 5 5 5
b) 5 5 junk value
c) 5 junk junk
d) Run time error
Solution: i) d ii) a
----------------------------------------------------------------2) Write a C program to add 2 numbers without using arithmetic and
logical operators.
Solution:
#include<stdio.h>
int Add(int x, int y){
while (y != 0){
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
int main(){
printf("%d", Add(15, 32));
return 0;
}
-----------------------------------------------------------------

3) Consider a m x n matrix. Find the number of paths from from the


leftmost top element to the rightmost bottom element. Diagonal paths
need not be considered.
Solution:
#include <iostream>
using namespace std;
int numberOfPaths(int m, int n){
int count[m][n];
for (int i = 0; i < m; i++)
count[i][0] = 1;
for (int j = 0; j < n; j++)
count[0][j] = 1;
for (int i = 1; i < m; i++){
for (int j = 1; j < n; j++)
count[i][j] = count[i-1][j] + count[i][j-1]; (+
count[i-1][j-1] should be used if diagonal elements are also to be
considered)
}
return count[m-1][n-1];
}
int main(){
cout << numberOfPaths(3, 3);
return 0;
}
----------------------------------------------------------------4) Sort an integer array that satisfies the following criteria:
a)Worst case space complexity is O(log(n))
b)Best case time complexity is O(n square)
Solution: a) QuickSort b) SelectionSort
Quick Sort:
#include<stdio.h>
// A utility function to swap two elements
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
/* This function takes last element as pivot, places

the pivot element at its correct position in sorted


array, and places all smaller (smaller than pivot)
to left of pivot and all greater elements to right
of pivot */
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)


{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);

/* The main function that implements QuickSort


arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[p] is now
at right place */
int pi = partition(arr, low, high);

// Separately sort elements before


// partition and after partition
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);

}
/* Function to print an array */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above functions

int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Selection Sort:
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
element
}

swap(&arr[min_idx], &arr[i]);
}

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above functions

int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
----------------------------------------------------------------5) Write a generic program in C or C++ to print the sequence

#include<bits/stdc++.h>
#define SIZE 10
using namespace std;
int main(void)
{
int L1_a, L2_a, L3_a, L4_a;
int L1_A[SIZE], L2_A[SIZE-1], L3_A[SIZE-2];
int i;
cin >> L1_a >> L2_a >> L3_a >> L4_a;
L3_A[0] = L3_a;
for(i=1;i<SIZE-2;i++)
L3_A[i] = L3_A[i-1] + L4_a;
L2_A[0] = L2_a;
for(i=1;i<SIZE-1;i++)
L2_A[i] = L2_A[i-1] + L3_A[i-1];
L1_A[0] = L1_a;
for(i=1;i<SIZE;i++)
L1_A[i] = L1_A[i-1] + L2_A[i-1];

for(i=0; i<SIZE; i++)


printf("%03d
printf("\n
");

",L1_A[i]);

for(i=0; i<SIZE-1; i++)


printf("%03d
printf("\n
");

",L2_A[i]);

for(i=0; i<SIZE-2; i++)


printf("%03d
printf("\n
");

",L3_A[i]);

for(i=0; i<SIZE-3; i++)


printf("%03d
printf("\n");

",L4_a);

return 0;
}
----------------------------------------------------------------6) Two strings str1 and str2 are called isomorphic if there is a one
to one mapping possible for every character of str1 to every character
of str2. And all occurrences of every character in 'str1' map to same
character in 'str2'
Input: str1 = "aab", str2 = "xxy"
Output: 1
'a' is mapped to 'x' and 'b' is mapped to 'y'
Input: str1 = "aab", str2 = "xyz"
Output: 0
One occurrence of 'a' in str1 has 'x' in str2 and other
occurrence of 'a' has 'y'
Solution:
#include<bits/stdc++.h>
using namespace std;
#define MAX_CHARS 256
bool areIsomorphic(string str1, string str2)
{
int m = str1.length(), n = str2.length();
if (m != n)
return false;
bool marked[MAX_CHARS] = {false};
int map[MAX_CHARS];
memset(map, -1, sizeof(map));

for (int i = 0; i < n; i++)


{
if (map[str1[i]] == -1)
{
if (marked[str2[i]] == true)
return false;
marked[str2[i]] = true;
map[str1[i]] = str2[i];
}
else if (map[str1[i]] != str2[i])
return false;
}
return true;

}
int main()
{
cout << areIsomorphic("aab", "xxy") << endl;
cout << areIsomorphic("aab", "xyz") << endl;
return 0;
}
-----------------------------------------------------------------

You might also like