Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the use of a semicolon (;) at the end of every program statement?

Answer»

It is majorly related to how the compiler reads( or parses) the entire code and breaks it into a set of instructions(or statements), to which semicolon in C acts as a boundary between two sets of instructions.


2.

Merge Two sorted Linked List

Answer»

Merge two sorted linked lists and return them as a sorted list. The list should be made by splicing TOGETHER the nodes of the first two lists.

Merging Two Sorted Linked ListNodePtr merge_sorted(NodePtr head1, NodePtr head2) { // if both lists are empty then merged list is also empty // if one of the lists is empty then other is the merged list if (head1 == nullptr) { return head2; } else if (head2 == nullptr) { return head1; } NodePtr mergedHead = nullptr; if (head1->data <= head2->data) { mergedHead = head1; head1 = head1->next; } else { mergedHead = head2; head2 = head2->next; } NodePtr mergedTail = mergedHead; while (head1 != nullptr && head2 != nullptr) { NodePtr temp = nullptr; if (head1->data <= head2->data) { temp = head1; head1 = head1->next; } else { temp = head2; head2 = head2->next; } mergedTail->next = temp; mergedTail = temp; } if (head1 != nullptr) { mergedTail->next = head1; } else if (head2 != nullptr) { mergedTail->next = head2; } return mergedHead;}

Runtime Complexity Linear, O(m + n) where m and n are lengths of both linked lists. 

Memory Complexity Constant, O(1)

MAINTAIN a head and a tail pointer on the merged linked list. Then choose the head of the merged linked list by comparing the first node of both linked lists. For all subsequent nodes in both lists, you choose the smaller current node and link it to the tail of the merged list, moving the current pointer of that list one step forward.

You keep doing this while there are some remaining elements in both lists. If there are STILL some elements in only one of the lists, you link this remaining list to the tail of the merged list.

Initially, the merged linked list is NULL. Compare the value of the first two nodes and make the node with the smaller value the head node of the merged linked list. In this example, it is 4 from head1.

Since it’s the first and only node in the merged list, it will also be the tail. Then move head1 one step forward.

Conclusion

C is the foundational language from which practically all other languages are built. C is the programming language's base. For writing system applications, it is a very POPULAR and frequently used language. Even if new languages have surpassed it in popularity, it remains one of the most popular programming languages. The C questions LISTED here will aid you in interviews as well as improve your learning. I hope you found these to be helpful!

Additional Interview Resources
  • C++ Interview Questions
  • Practice Coding
  • Difference Between C and C++
  • Features of C Language
  • C Programming MCQs
  • C IDE
  • C Projects
  • Technical Interview Questions
  • Coding Interview Questions
3.

Write a program to find the node at which the intersection of two singly linked lists begins.

Answer»

Let's take an EXAMPLE of the following two linked lists which intersect at node c1.

Intersection of Two Linked List

Solution -

  • Get count of the NODES in the first list, let count be c1.
  • Get count of the nodes in the second list, let count be c2.
  • Get the difference of counts d = abs(c1 – c2)
  • Now traverse the bigger list from the first node till d nodes so that from here ONWARDS both the lists have an equal no of nodes
  • Then we can traverse both the lists in parallel till we COME across a common node. (Note that getting a common node is done by comparing the address of the nodes)
// Function to get the intersection point // of the given linked lists int getIntersectionNode(Node* head1, Node* head2) { Node *curr1 = head1, *curr2 = head2; // While both the pointers are not equal while (curr1 != curr2) { // If the first POINTER is null then // set it to point to the head of // the second linked list if (curr1 == NULL) { curr1 = head2; } // Else point it to the next node else { curr1 = curr1->next; } // If the second pointer is null then // set it to point to the head of // the first linked list if (curr2 == NULL) { curr2 = head1; } // Else point it to the next node else { curr2 = curr2->next; } } // Return the intersection node return curr1->data; }
4.

Program to find n’th Fibonacci number

Answer»

Fibonacci sequence is characterized by the fact that every number after the first two is the sum of the two preceding ONES. For example, consider below sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . .. and so on

Where in F{n} = F{n-1} + F{n-2} with base VALUES F(0) = 0 and <code>F(1) = 1

Below is NAIVE IMPLEMENTATION for finding the NTH member of the Fibonacci sequence

// Function to find the nth Fibonacci numberint fib(int n){ if (n <= 1) { return n; } return fib(n - 1) + fib(n - 2);} int main(){ int n = 8; printf("nth Fibonacci number is %d", fib(8)); return 0;}
5.

Check for Balanced Parentheses using Stack

Answer»

Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

  • Open brackets must be closed by the same type of brackets.
  • Open brackets must be closed in the correct order.

Example 1:
 Input: s = "()"
 Output: true

Example 2:
 Input: s = "()[]{}"
 Output: true

Example 3:
 Input: s = "(]"
 Output: false

Below is the source code for C Program to Check for Balanced PARENTHESES using Stack which is successfully compiled and run on Windows System to produce desired output as shown below :

int check(char EXP[] ){ int i; char temp; for(i=0;i<strlen(exp);i++) { if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[') PUSH(exp[i]); if(exp[i]==')' || exp[i]=='}' || exp[i]==']') if(top==-1) /*stack empty*/ { printf("Right parentheses are more than left parentheses\n"); return 0; } else { temp=pop(); if(!match(temp, exp[i])) { printf("Mismatched parentheses are : "); printf("%c and %c\n",temp,exp[i]); return 0; } } } if(top==-1) /*stack empty*/ { printf("Balanced Parentheses\n"); return 1; } else { printf("Left parentheses more than right parentheses\n"); return 0; }}
6.

Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL

Answer»

Assume that we have linked list 1 → 23 → Ø, we would like to change it to Ø ← 1 ← 2 ← 3.

While you travel the linked list, change the current node's next pointer to point to its previous element. reference to the previous nodes should be stored into a temp variable as shown so that we don’t lose track of the swapped node. You also need another pointer to STORE the next node before changing the reference. Also when we are done return the new head of the REVERSED list.

/* Function to reverse the linked list */static VOID reverse(struct Node** head_ref) { struct Node* prev = NULL; struct Node* current = *head_ref; struct Node* next; while (current != NULL) { // store next next = current->next; // reverse curr node pointer current->next = prev; // move pointer one position ahead prev = current; current = next; } *head_ref = prev; }
7.

Check whether the number is EVEN or ODD, without using any arithmetic or relational operators

Answer»

#include<stdio.h>INT MAIN(){ int x; PRINTF("Enter a number: "); scanf("%d", &x); (x&1)?printf("ODD"):printf("Even"); return 0;}

The bitwise and(&) operator can be used to quickly CHECK the number is odd or even.

8.

Multiply an Integer Number by 2 Without Using Multiplication Operator

Answer»

#include<stdio.h>int main(){ int x; printf("Enter a number: "); scanf("%d",&AMP;x); printf("%d", x<<1); return 0;}

The left shift OPERATOR shifts all bits towards the left by a certain number of specified bits. The expression x<<1 always returns x*2. Note that the shift operator doesn’t work on floating-point values.

For multiple of x by 4, use x<<2. Similarly x<<3 MULTIPLY x by 8. For multiple of the number x by 2^n, use x<<n.

9.

Subtract Two Number Without Using Subtraction Operator

Answer»

#include&LT;stdio.h>#include<stdlib.h>int main(){ int X, y; printf("Enter two number: "); scanf("%d %d",&x,&y); printf("%d", x+(~y)+1); return 0;}

The BITWISE COMPLEMENT OPERATOR is used in this program. The bitwise complement of number ~y=-(y+1). So, expression will become x+(-(y+1))+1=x-y-1+1=x-y

10.

Add Two Numbers Without Using the Addition Operator

Answer»

For the SUM of two numbers, we use the addition (+) operator. In these tricky C programs, we will write a C program to add two numbers without using the addition operator.

#include&LT;stdio.h>#include<stdlib.h>int MAIN(){ int X, y; printf("Enter two number: "); scanf("%d %d",&x,&y); // METHOD 1 printf("%d\n", x-(-y)); // method 2 printf("%d\n", -(-x-y)); // method 3 printf("%d\n", abs(-x-y)); // method 4 printf("%d", x-(~y)-1); return 0;}
11.

What is dynamic data structure?

Answer»

A dynamic data structure (DDS) refers to an organization or collection of data in memory that has the flexibility to grow or shrink in size, enabling a programmer to CONTROL EXACTLY how much memory is utilized. Dynamic data structures change in size by having unused memory allocated or de-allocated from the heap as NEEDED

Dynamic data structures play a key role in programming LANGUAGES like C, C++, and Java because they provide the programmer with the flexibility to adjust the memory CONSUMPTION of software programs.

12.

When is the "void" keyword used in a function

Answer»

The keyword “void” is a data type that literally represents no data at all. The most obvious USE of this is a FUNCTION that RETURNS nothing:

void PrintHello() { PRINTF("Hello\n"); return; // the function does "return", but no value is returned }

Here we’ve declared a function, and all functions have a return type. In this case, we’ve said the return type is “void”, and that means, “no data at all” is returned. 
The other use for the void keyword is a void pointer. A void pointer points to the memory location where the data type is undefined at the time of variable definition. EVEN you can define a function of return type void* or void pointer meaning “at compile time we don’t know what it will return” Let’s see an example of that.

void MyMemCopy(void* dst, const void* src, int numBytes) { char* dst_c = reinterpret_cast<char*>(dst); const char* src_c = reinterpret_cast<const char*>(src); for (int i = 0; i < numBytes; ++i) dst_c[i] = src_c[i]; }
13.

What are header files and what are its uses in C programming?

Answer»

Header Files in C

In C header files must have the extension as .h, which contains function definitions, data TYPE definitions, macro, ETC. The header is useful to import the above definitions to the source code USING the #include directive. For example, if your source code needs to take input from the user do some manipulation and print the OUTPUT on the terminal, it should have stdio.h file included as #include <stdio.h&GT;, with which we can take input using scanf() do some manipulation and print using printf().

14.

Differentiate Source Codes from Object Codes

Answer»

Source Code and Object Code Difference

The difference between the Source Code and Object Code is that Source Code is a collection of computer instructions WRITTEN using a human-readable programming language while Object Code is a sequence of statements in machine language, and is the OUTPUT after the compiler or an assembler converts the Source Code.

The last POINT about Object Code is the way the changes are REFLECTED. When the Source Code is modified, each time the Source Code needs to be compiled to REFLECT the changes in the Object Code.

15.

Differentiate between the macros and the functions.

Answer»

The differences between MACROS and functions can be EXPLAINED as follows:

MacrosFunctions
It is preprocessed rather than compiled.It is compiled not preprocessed.
It is preprocessed rather than compiled.Function CHECKS for compilation errors.
CODE length is increased.Code length remains the same.
Macros are faster in execution.Functions are a bit slower in execution.
Macros are useful when a small piece of code is used multiple times in a program.Functions are helpful when a large piece of code is repeated a number of times.
16.

How to call a function before main()?

Answer»

To call a FUNCTION before the MAIN(), pragma startup directive should be used. E.g.-

#pragma startup funvoid FUN(){printf("In fun\n");}main(){printf("In main\n");}

The output of the above program will be -

In funIn main

This pragma directive, on the other hand, is compiler-dependent. This is not SUPPORTED by gcc. As a result, it will ignore the startup directive and produce no error. But the output, in that case, will be -

In main
17.

Can you tell me how to check whether a linked list is circular?

Answer»

Single Linked List

Single Linked List

CIRCULAR Linked List

Circular linked list is a variation of a linked list where the last node is pointing to the first node's information part. Therefore the last node does not point to null.

Algorithm to find whether the given linked list is circular

A very simple way to determine whether the linked list is circular or not

  • Traverse the linked list
  • Check if the node is pointing to the head.
  • If yes then it is circular.

Let's look at the snippet where we code this algorithm.

Create a structure for a linked listDeclare-Variable to store data of the node.-Pointer variable of struct type to store the address of next node.function of datatype tool isCircular(firstgode){-Store the value of first node in temp variable and make it traverse all nodes.-temp-firstgode-tempenext node pointed by temp(temp-&GT;next)-run until temp is at null or firstNodeif (temp at null) not circular and returns false if (temp points first node) return true as its circular. } function of datatype node newNode(data){-To insert NEW nodes and link each one of them to the previous node by storing the address of the new node to the previous one.-Then make them point to NULL.}In int main function-First insert nodes for circular linked list and check its nature by calling isCircular function.-Since it is true through if statement it prints "yes..-Second insert a normal linked list and check its nature by calling isCircular function. As its not circular it prints "no",",can-you-tell-me-how-to-check-whether-a-linked-list-is-circular,linked list 7539,55201,C INTERVIEW QUESTIONS|C INTERVIEW QUESTIONS FOR EXPERIENCED IN C INTERVIEW QUESTIONS,C Interview Questions,C Interview Questions For Experienced in C Interview Questions,What is the use of a semicolon (;) at the end of every program statement?,"

It is majorly related to how the compiler reads( or parses) the entire code and breaks it into a set of instructions(or statements), to which semicolon in C acts as a boundary between TWO sets of instructions.

18.

C Program to find a sum of digits of a number using recursion.

Answer»

#INCLUDE<stdio.h>#include<conio.h>int sumOfDigits(int num) { STATIC int sum = 0; int rem; sum = sum + (num%10); rem = num/10; if(rem > 0) { sumOfDigits(rem); } return sum; }int main() { int j,num; printf("Please enter a number :"); scanf("%d",&num); printf("sum of digits of the number = %d ",sumOfDigits(num)); getch();}

19.

C program to check the given number format is in binary or not.

Answer»

#include<stdio.h>#include<conio.h>INT main() { int J,num; printf("Please ENTER a number :"); SCANF("%d",&num); while(num>0) { j=num%10; if( j!=0 && j!=1 ) { printf("num is not binary"); break; } num=num/10; if(num==0) { printf("num is binary"); } } getch();}

20.

Write a C program to check if it is a palindrome number or not using a recursive method.

Answer»

#include &LT;stdio.h>#include <conio.h>int reverse(int num);int ISPALINDROME(int num);int main(){ int num; printf("Enter a NUMBER: "); scanf("%d", &num);if(isPalindrome(num) == 1){ printf("the given number is a palindrome"); } else { printf("the given number is not a palindrome number"); } return 0;}int isPalindrome(int num){ if(num == reverse(num)) { return 1; } return 0;}int reverse(int num){int rem; static int SUM=0; if(num!=0){ rem=num%10; sum=sum*10+rem; reverse(num/10); } else return sum; return sum;}

21.

How do you override a defined macro?

Answer»

To override a defined macro we can USE #ifdef and #undef preprocessors as follows:

  • #ifdef A
  • #undef A
  • #endif
  • #define A 10

If macro A is defined, it will be undefined USING undef and then defined again using define.

22.

Write a program to get the higher and lower nibble of a byte without using shift operator?

Answer»

#include<stdio.h>STRUCT full_byte{char first : 4;char second : 4;};union A{char x;struct full_byte by;};MAIN(){char c = 100;union A a;a.x = c;PRINTF("the two nibbles are: %d and %d\n", a.by.first, a.by.second);}

23.

Can we compile a program without a main() function?

Answer»

Yes, we can COMPILE a program WITHOUT MAIN() function USING Macro.

E.g.

#include<studio.h>#define abc mainint abc (){printf("Hello World ");return 0;}
24.

How can you remove duplicates in an array?

Answer»

The FOLLOWING program will HELP you to remove duplicates from an ARRAY.

#include <stdio.h>int main(){ int N, a[100], b[100], calc = 0, i, j,count; printf("Enter no. of elements in array.n"); scanf("%d", &n); printf("Enter %d integersn", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); for (i = 0; i<n; i++) { for (j = 0; j<calc; j++) { if(a[i] == b[j]) break; } if (j== calc) { b[count] = a[i]; calc++; } } printf("Array obtained after REMOVING duplicate elements"); for (i = 0; i<calc; i++) { printf("%dn", b[i]); } return 0;}