# What are C programing interview questions?

subarnabasnet 18 Nov 2023 | 11:07 pm 1. Swap two variables without using a temporary variable: ``` a = a + b b = a - b a = a - b ```
2. 8 Queens Problem:

Refer to a specific algorithm or backtracking technique designed for the 8-Queens problem.

3. 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]) ```
4. Reverse a string: ``` start = 0 end = length_of_string - 1 while start < end: swap characters at start and end increment start decrement end ```
5. Reverse words in a sentence in place: ``` reverse the entire sentence reverse each word in the reversed sentence ```
6. Generate permutations:

Use a recursive algorithm to generate permutations.

7. Calculate the factorial of a number: ``` factorial = 1 for i from 1 to n: factorial = factorial * i ```
8. Calculate pow(x, n):

Implement a power function using recursion or iteration.

9. Wildcard pattern matching algorithm:

Implement a simple pattern matching algorithm or use regular expressions.

10. Calculate the maximum subarray:

Use Kadane's algorithm to find the maximum sum subarray.

11. 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.

12. Solve the Rat In A Maze problem using backtracking:

Implement a backtracking algorithm to solve the Rat In A Maze problem.

13. Little-Endian and Big-Endian:

Determine endianness using a union or bitwise operations. Use conversion functions to switch between little and big-endian.

14. Tower of Hanoi problem:

Implement a recursive solution to the Tower of Hanoi problem.

15. Return a string from a function:

Implement a function that returns a string.

16. Produce own source code as output:

Use a self-replicating code technique.

17. Convert from decimal to any base:

Implement a function to convert a decimal number to any specified base.

18. Check if an integer is a power of 2: ``` is_power_of_2 = (num & (num - 1)) == 0 ```
19. Find the GCD of two numbers:

Implement the Euclidean algorithm.

20. Finding a duplicated integer problem:

Implement a solution to find duplicated integers in an array.

21. 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] ```
22. Find the maximum of three integers using the ternary operator: ``` max_value = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c) ```
23. How do you initialize a pointer inside a function:

Initialize the pointer using `malloc` or allocate memory using `new` (in C++).

24. Write C code to dynamically allocate one, two and three-dimensional arrays (using malloc()):

Use `malloc` to allocate memory for arrays.

25. How would you find the size of structure without using sizeof():

Calculate the size manually by adding the size of each member.

26. Write a C program to multiply two matrices:

Implement a nested loop to perform matrix multiplication.

27. Write a C program to check for palindromes:

Compare characters from the beginning and end of the string until they meet in the middle.

28. Write a C program to convert a decimal number into a binary number:

Use repeated division by 2 and store remainders in reverse order.

29. Write C code to implement the Binary Search algorithm:

Implement a binary search algorithm on a sorted array.

30. Write code to evaluate a polynomial:

Use a loop to iterate through the terms of the polynomial and calculate the result.

31. Write code to add two polynomials:

Combine like terms by adding coefficients.

32. Write a program to add two long positive numbers (each represented by linked lists):

33. How do you compare floating-point numbers?:

Use epsilon value for a small allowable difference.

34. What's a good way to implement complex numbers in C?:

Use a structure to represent a complex number.

35. How can I display a percentage-done indication on the screen?:

Print a progress bar or percentage completed during a task.

36. 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.

37. Is there something we can do in C but not in C++?:

Explore features or scenarios unique to C.

38. How to swap the two nibbles in a byte?: ``` result = ((num & 0x0F) * (num >> 4)) & 0x0F ```
39. How to scan a string till we hit a new line using scanf()?:

Use `%[^\\n]` in the `scanf` format specifier.

40. Write pseudocode to compare versions (like 115.10.1 vs 115.11.5):

Split the versions into numbers and compare each segment.

41. How do you get the line numbers in C?:

Use the `__LINE__` macro.

42. How to fast multiply a number by 7?: ``` result = (num << 3) - num ```
43. 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.

44. Is there a way to multiply matrices in lesser than O(n^3) time complexity?:

Use Strassen's algorithm for matrix multiplication.

45. How do you find out if a machine is 32 bit or 64 bit?:

Check the size of pointers.

46. 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.

47. Write code to round numbers:

Use the `round()` function or add 0.5 and cast to an integer.

48. How can we sum the digits of a given number in a single statement?: ``` sum = (num % 9) + (num && 9) ```
49. 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.

50. 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.

51. Write a program to check if the stack grows up or down:

52. How to add two numbers without using the plus operator?:

Use bitwise operations.

53. 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.

54. Write a program to print numbers from 1 to 100 without using loops!:

Use recursion or other creative methods.

55. Write your own trim() or squeeze() function to remove the spaces from a string.:

Implement a function to remove spaces from a string.

56. Write your own random number generator function in C.:

Implement a simple random number generator.

57. Write your own sqrt() function in C:

Implement a square root function. 