Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

SBT CODING SOLUTION 18 JUNE

 

 **  SBT CODING SOLUTION 18 JUNE ** 

** CLICK ON THE ADS ** 

turned on gray laptop computer

TECHNICAL MCQ:

1.  c

2.  d

3.  b

4.  a

5.  a

6.  b

7.  c

8.  a

9.  a

10. b

 

***************************************************************

PROGRAMMING :

PROBLEM 1

Problem Statement
Given a singly linked list and a key, count the number of occurrences of given key in the linked list.
Input Format
First line of input contains n the size of linked list. Next line of input contains n integers the elements of
linked list Next line of input contains x whose occurence is needed to be count.
Constraints
0 ≤ N ≤ 10^4
Output Format
Print the number of occurrences of given key in the linked list

 

SOLUTION : 

PYTHON CODE : 

n=int(input())
m=list(map(int,input().split()))
x=int(input())
print(m.count(x))



PROBLEM 2

Problem Statement
Given a singly linked list of size N. The task is to swap elements in the linked list pairwise. For example, if
the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3. Note: You need to swap the nodes,
not only the data. If only data is swapped then driver will print -1.
Input Format
First line of input contains n the size of linked list. Next line of input contains n integers the elements of
linked list
Constraints
1 ≤ N ≤ 10^3
Output Format
Print the swapped elements of linked list.

 

SOLUTION :

C CODE :

#include <stdio.h>
#include <stdlib.h>

// A Linked List Node
struct Node
{
    int data;
    struct Node *next;
};

// Helper function to insert a new node at the beginning of the linked list
void push(struct Node** headRef, int data)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;
    newNode->next = *headRef;
    *headRef = newNode;
}

// Helper function to print the linked list
void printList(struct Node *node)
{
    while (node)
    {
        printf("%d ", node->data);
        node = node->next;
    }
}

// Function to pairwise swap adjacent nodes of a linked list
void rearrange(struct Node **headRef)
{
    // if the list is empty or contains just one node
    if (*headRef == NULL || (*headRef)->next == NULL) {
        return;
    }

    struct Node* curr = *headRef, *prev = NULL;

    // consider two nodes at a time and swap their links
    while (curr != NULL && curr->next != NULL)
    {
        struct Node* temp = curr->next;
        curr->next = temp->next;
        temp->next = curr;

        if (prev == NULL) {
            *headRef = temp;
        }
        else {
            prev->next = temp;
        }

        prev = curr;
        curr = curr->next;
    }
}

int main(void)
{int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
{
  scanf("%d ",&arr[i]);
}

    struct Node* head = NULL;
    for (int i = n - 1; i >= 0; i--) {
        push(&head, arr[i]);
    }
    rearrange(&head);
    printList(head);

    return 0;
}

 

 

PROBLEM 3

Problem Statement
Given a Cirular Linked List of size N, split it into two halves circular lists. If there are odd number of
nodes in the given circular linked list then out of the resulting two halved lists, first list should have one
node more than the second list. The resultant lists should also be circular lists and not linear lists.
Input Format
First line of input contains n the size of linked list. Next line of input contains n integers the elements of
linked list.
Constraints
1 <= N <= 100
Output Format
Print the two halved circular linked list.

 

SOLUTION :

C++ CODE : 

#include <iostream>
using namespace std;

struct node {
    int num;
    struct node * next;
}*head;
void build(int n)
{
    int i, num;
    struct node *preptr, *newnode;

    if(n >= 1)
    {
        head = (struct node *)malloc(sizeof(struct node));
        cin>>num;
        head->num = num;
        head->next = NULL;
        preptr = head;
        for(i=2; i<=n; i++)
        {
            newnode = (struct node *)malloc(sizeof(struct node));
            cin>>num;
            newnode->num = num;
            newnode->next = NULL;   
            preptr->next = newnode;   
            preptr = newnode;          
        }
        preptr->next = head;
    }
}
void display(struct node *head)
{
    struct node *tmp;
    int n = 1;
    
    if(head == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        tmp = head;
        
        
        do {
            cout<<tmp->num<<" ";
            tmp = tmp->next;
            n++;
        }while(tmp != head);
    }
}
void splitList( struct node **head1,  struct node **head2)
{
  struct node *slow = head;
  struct node *fast = head;  
 
  if(head == NULL)
    return;
   
  while(fast->next != head && fast->next->next != head)
  {
     fast = fast->next->next;
     slow = slow->next;
  }   
 
  if(fast->next->next == head)
    fast = fast->next;       
    
  *head1 = head;     
      
    if(head->next != head)
    *head2 = slow->next;
    
   
  fast->next = slow->next;
    
     
  slow->next = head;        
}

int main()
{
    int n;
    struct node *head1 = NULL;
    struct node *head2 = NULL;
    head = NULL;
    cin>>n;
    build(n);
    splitList(&head1, &head2);
    display(head1);
    cout<<'\n';
    display(head2);
    return 0;
}

 



***********************************************************************



Post a Comment

Previous Post Next Post