Luxsoft Interview questions
Find the Normal of a square matrix = Sqrt (sum of squares of all elements of the matrix )
[ 1 2 3]
[ 2 3 4]
[ 400000 500000 6]
[ 1 2 3 2 3 4 4 5 6 ]
double find_normal_square_matrix(int arr[][],int num)
{
long long sum=0;
int k=0;
for(i=0; i< num*num; i++)
{
int j= i%num;
if(i%num == 0)
{ j = 0;
k++;
}
temp = arr[k][j];
sum+=(long long)temp*temp;
}
return sqrt(sum);
}
Write a simple C++ program to delete a node in the single linked list when the pointer to the node is given as input (no other) )with all scenarios considered
iferoz@luxoft.com
Solution:- copy all the next nodes data to itself and delete the last node
void node_del(struct node* ptr)
{
if(ptr->next == NULL)
{
free(ptr);
return;
}
struct node* t = ptr->next;
ptr->data = t->data;
ptr->next = t->next;
free(t);
}
What will happen to the second last node if last node gets deleted ?
Ans:- will the second last node's next become a dangling pointer ! Yes
How to solve the dangling pointer issue ?
Write a C++ program for finding maximal subarray given an array as input. The input is an array & its size and the o/p of the program will be 2 indices - lower and upper index between which is the max sub array is present.
E.g.
A[10] = { 1, -4, -1, 3, 4, 5, -17, 8, 1,2 }
Here the max subarray is { 3, 4, 5} ans: 3, 5 (indices)
E.g 2
A[10] = { 1, -4, -1, 3, 4, 5, -7, 8, 1,2 }
Here the max subarray is {3, 4, 5, -7, 8, 1,2 } ans: 3, 9 (indices)
Please handle all scenarios / cases… and print the array also
int max_subarray(int arr[], int size)
{
int max_1 = 0, max_2 = -60000;
for(int i=0; i<size; i++)
{
max_1 = max_1 + arr[i];
if( max_1 < arr[i] )
{ max_1 = arr[i];
begin = i;
}
if(max_2 < max_1)
{
max_2 = max_1;
start_index= begin;
final_index =i;
}
}
for(i=start_index; i<final_index; i++)
printf("%d ", arr[i]);
return max_2;
}
Comments
Post a Comment