What are C programing interview questions?

-
Swap two variables without using a temporary variable:
a = a + b b = a - b a = a - b
-
8 Queens Problem:
Refer to a specific algorithm or backtracking technique designed for the 8-Queens problem.
-
Print a square matrix helically:
for i from 0 to n-1: for j from i to n-i-1: print(matrix[i][j]) for j from i+1 to n-i-1: print(matrix[j][n-i-1]) for j from n-i-2 down to i: print(matrix[n-i-1][j]) for j from n-i-2 down to i+1: print(matrix[j][i])
-
Reverse a string:
start = 0 end = length_of_string - 1 while start < end: swap characters at start and end increment start decrement end
-
Reverse words in a sentence in place:
reverse the entire sentence reverse each word in the reversed sentence
-
Generate permutations:
Use a recursive algorithm to generate permutations.
-
Calculate the factorial of a number:
factorial = 1 for i from 1 to n: factorial = factorial * i
-
Calculate pow(x, n):
Implement a power function using recursion or iteration.
-
Wildcard pattern matching algorithm:
Implement a simple pattern matching algorithm or use regular expressions.
-
Calculate the maximum subarray:
Use Kadane's algorithm to find the maximum sum subarray.
-
Generate Fibonacci numbers and check if a number is Fibonacci:
Implement a function to generate Fibonacci numbers and check if a given number is in the Fibonacci sequence.
-
Solve the Rat In A Maze problem using backtracking:
Implement a backtracking algorithm to solve the Rat In A Maze problem.
-
Little-Endian and Big-Endian:
Determine endianness using a union or bitwise operations. Use conversion functions to switch between little and big-endian.
-
Tower of Hanoi problem:
Implement a recursive solution to the Tower of Hanoi problem.
-
Return a string from a function:
Implement a function that returns a string.
-
Produce own source code as output:
Use a self-replicating code technique.
-
Convert from decimal to any base:
Implement a function to convert a decimal number to any specified base.
-
Check if an integer is a power of 2:
is_power_of_2 = (num & (num - 1)) == 0
-
Find the GCD of two numbers:
Implement the Euclidean algorithm.
-
Finding a duplicated integer problem:
Implement a solution to find duplicated integers in an array.
-
Write code to remove duplicates in a sorted array:
unique_index = 0 for i from 1 to n-1: if array[i] != array[unique_index]: unique_index++ array[unique_index] = array[i]
-
Find the maximum of three integers using the ternary operator:
max_value = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)
-
How do you initialize a pointer inside a function:
Initialize the pointer using
malloc
or allocate memory usingnew
(in C++). -
Write C code to dynamically allocate one, two and three-dimensional arrays (using malloc()):
Use
malloc
to allocate memory for arrays. -
How would you find the size of structure without using sizeof():
Calculate the size manually by adding the size of each member.
-
Write a C program to multiply two matrices:
Implement a nested loop to perform matrix multiplication.
-
Write a C program to check for palindromes:
Compare characters from the beginning and end of the string until they meet in the middle.
-
Write a C program to convert a decimal number into a binary number:
Use repeated division by 2 and store remainders in reverse order.
-
Write C code to implement the Binary Search algorithm:
Implement a binary search algorithm on a sorted array.
-
Write code to evaluate a polynomial:
Use a loop to iterate through the terms of the polynomial and calculate the result.
-
Write code to add two polynomials:
Combine like terms by adding coefficients.
-
Write a program to add two long positive numbers (each represented by linked lists):
Traverse both linked lists, adding corresponding digits, and handle carry.
-
How do you compare floating-point numbers?:
Use epsilon value for a small allowable difference.
-
What's a good way to implement complex numbers in C?:
Use a structure to represent a complex number.
-
How can I display a percentage-done indication on the screen?:
Print a progress bar or percentage completed during a task.
-
Write a program to check if a given year is a leap year or not?:
Check if the year is divisible by 4, not divisible by 100 unless divisible by 400.
-
Is there something we can do in C but not in C++?:
Explore features or scenarios unique to C.
-
How to swap the two nibbles in a byte?:
result = ((num & 0x0F) * (num >> 4)) & 0x0F
-
How to scan a string till we hit a new line using scanf()?:
Use
%[^\\n]
in thescanf
format specifier. -
Write pseudocode to compare versions (like 115.10.1 vs 115.11.5):
Split the versions into numbers and compare each segment.
-
How do you get the line numbers in C?:
Use the
__LINE__
macro. -
How to fast multiply a number by 7?:
result = (num << 3) - num
-
Write a simple piece of code to split a string at equal intervals:
Use a loop to iterate through the string and print characters at specified intervals.
-
Is there a way to multiply matrices in lesser than O(n^3) time complexity?:
Use Strassen's algorithm for matrix multiplication.
-
How do you find out if a machine is 32 bit or 64 bit?:
Check the size of pointers.
-
Write a program to have the output go two places at once (to the screen and to a file also):
Use file redirection or logging.
-
Write code to round numbers:
Use the
round()
function or add 0.5 and cast to an integer. -
How can we sum the digits of a given number in a single statement?:
sum = (num % 9) + (num && 9)
-
Given two strings A and B, how would you find out if the characters in B were a subset of the characters in A?:
Use a hash table or check each character of B against A.
-
Write a program to merge two arrays in sorted order, so that if an integer is in both the arrays, it gets added into the final array only once:
Merge while checking for duplicates.
-
Write a program to check if the stack grows up or down:
Compare the addresses.
-
How to add two numbers without using the plus operator?:
Use bitwise operations.
-
How to generate prime numbers? How to generate the next prime after a given prime?:
Implement a function to generate prime numbers and find the next prime after a given prime.
-
Write a program to print numbers from 1 to 100 without using loops!:
Use recursion or other creative methods.
-
Write your own trim() or squeeze() function to remove the spaces from a string.:
Implement a function to remove spaces from a string.
-
Write your own random number generator function in C.:
Implement a simple random number generator.
-
Write your own sqrt() function in C:
Implement a square root function.
