🔷 Intermediate Question 1: Check Armstrong Number
🧩 Problem Statement:
Write a program to check whether a number is an Armstrong number or not.
An n-digit number is Armstrong if the sum of its digits raised to the power n equals the number itself.
Example:
Input: 153 → Output: Armstrong Number
(1^3 + 5^3 + 3^3 = 153)
💻 Code:
#include
#include
int main() {
int num, original, remainder, n = 0;
float result = 0.0;
printf(“Enter an integer: “);
scanf(“%d”, &num);
original = num;
// Count number of digits
for (int temp = num; temp != 0; ++n) {
temp /= 10;
}
// Calculate the sum of nth power of each digit
for (int temp = num; temp != 0; temp /= 10) {
remainder = temp % 10;
result += pow(remainder, n);
}
if ((int)result == original)
printf(“%d is an Armstrong number.\n”, original);
else
printf(“%d is not an Armstrong number.\n”, original);
return 0;
}
🧠 Explanation:
We first count the number of digits, then calculate the sum of each digit raised to that power. If the final result matches the original number, it's an Armstrong number.
🔷 Intermediate Question 2: Fibonacci Series (Without Recursion)
🧩 Problem Statement:
Write a program to print the first n terms of the Fibonacci series without using recursion.
Example:
Input: 5 → Output: 0 1 1 2 3
💻 Code:
#include
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf(“Enter number of terms: “);
scanf(“%d”, &n);
printf(“Fibonacci Series: %d %d “, t1, t2);
for (int i = 3; i <= n; ++i) {
nextTerm = t1 + t2;
printf(“%d “, nextTerm);
t1 = t2;
t2 = nextTerm;
}
return 0;
}
🧠 Explanation:
We initialize the first two terms, then loop through to generate each new term by summing the previous two, shifting the variables forward each time.
🔷 Intermediate Question 3: Find GCD of Two Numbers
🧩 Problem Statement:
Write a program to find the Greatest Common Divisor (GCD) of two given integers.
Example:
Input: 60, 48 → Output: 12
💻 Code:
#include
int main() {
int a, b;
printf(“Enter two numbers: “);
scanf(“%d %d”, &a, &b);
while (a != b) {
if (a > b)
a -= b;
else
b -= a;
}
printf(“GCD is %d\n”, a);
return 0;
}
🧠 Explanation:
We use the subtraction-based Euclidean algorithm here. We subtract the smaller number from the larger until both are equal, at which point the result is the GCD.
🔷 Intermediate Question 4: Print Pascal’s Triangle
🧩 Problem Statement:
Write a C program to print Pascal's Triangle of a given height.
Pascal’s Triangle looks like this for height 5:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
💻 Code:
#include
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n – 1);
}
int combination(int n, int r) {
return factorial(n) / (factorial(r) * factorial(n – r));
}
int main() {
int i, j, rows;
printf(“Enter number of rows: “);
scanf(“%d”, &rows);
for (i = 0; i < rows; i++) {
for (j = 0; j <= rows – i – 1; j++)
printf(” “);
for (j = 0; j <= i; j++)
printf(“%4d”, combination(i, j));
printf(“\n”);
}
return 0;
}
🧠 Explanation:
We use a factorial-based approach to calculate combinations (nCr). Proper formatting helps center-align the triangle using spaces.
🔶 Advanced Question 1: Implement a Basic LRU Cache
🧩 Problem Statement:
Write a C program to implement a simplified version of an LRU (Least Recently Used) Cache using arrays. The cache should remove the least recently used item when it exceeds the defined size.
💻 Code:
#include
#include
#define SIZE 3
int cache[SIZE];
int count = 0;
void accessPage(int page) {
int found = 0;
for (int i = 0; i < count; i++) {
if (cache[i] == page) {
found = 1;
for (int j = i; j < count - 1; j++) {
cache[j] = cache[j + 1];
}
break;
}
}
if (count < SIZE)
cache[count++] = page;
else {
for (int i = 0; i < SIZE - 1; i++) {
cache[i] = cache[i + 1];
}
cache[SIZE - 1] = page;
}
printf("Cache: ");
for (int i = 0; i < count; i++)
printf("%d ", cache[i]);
printf("\n");
}
int main() {
int pages[] = {1, 2, 3, 1, 4, 5};
int n = sizeof(pages) / sizeof(pages[0]);
for (int i = 0; i < n; i++) {
accessPage(pages[i]);
}
return 0;
}
🧠 Explanation:
This simple LRU cache simulates recent accesses. When a page is reaccessed, it’s moved to the end; if the cache exceeds capacity, the least recently used one (first element) is removed.
🔶 Advanced Question 2: Detect a Loop in Linked List
🧩 Problem Statement:
Write a C program to detect if a loop exists in a singly linked list using Floyd's Cycle Detection algorithm.
💻 Code:
#include
#include
struct Node {
int data;
struct Node* next;
};
void detectLoop(struct Node* head) {
struct Node *slow = head, *fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast) {
printf("Loop detected in the linked list.\n");
return;
}
}
printf("No loop in the linked list.\n");
}
int main() {
struct Node *head = malloc(sizeof(struct Node));
struct Node *second = malloc(sizeof(struct Node));
struct Node *third = malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = second; // Creating loop
detectLoop(head);
return 0;
}
🧠 Explanation:
This code uses two pointers (slow and fast). If there's a loop, the fast pointer will eventually meet the slow one—an elegant and widely used technique in system-level C programming.
🔶 Advanced Question 3: Multithreading Simulation with Fork
🧩 Problem Statement:
Write a C program to simulate two processes using fork(). Let the parent print even numbers and the child print odd numbers from 1 to 10.
💻 Code:
#include
#include
#include
int main() {
pid_t pid = fork();
if (pid < 0) {
printf("Fork failed!\n");
} else if (pid == 0) {
printf("Child Process (Odd Numbers):\n");
for (int i = 1; i <= 10; i += 2)
printf("%d ", i);
printf("\n");
} else {
wait(NULL);
printf("Parent Process (Even Numbers):\n");
for (int i = 2; i <= 10; i += 2)
printf("%d ", i);
printf("\n");
}
return 0;
}
🧠 Explanation:
We use fork() to split into child and parent processes. Each process executes its respective logic (odds for child, evens for parent). This is foundational for understanding process creation in system-level programming.
🔶 Advanced Question 4: Implement a Custom Memory Allocator (Very Simple Malloc)
🧩 Problem Statement:
Simulate a basic memory allocator using a static array in C. Allocate and deallocate memory using your own logic.
💻 Code:
#include
#include
#define MEMORY_SIZE 10
int memory[MEMORY_SIZE];
bool used[MEMORY_SIZE];
void* my_malloc(int size) {
for (int i = 0; i < MEMORY_SIZE - size + 1; i++) {
bool free_block = true;
for (int j = i; j < i + size; j++) {
if (used[j]) {
free_block = false;
break;
}
}
if (free_block) {
for (int j = i; j < i + size; j++)
used[j] = true;
return &memory[i];
}
}
return NULL;
}
void my_free(void* ptr) {
int index = ((int*)ptr - memory);
used[index] = false;
}
int main() {
int* ptr1 = (int*)my_malloc(2);
if (ptr1 != NULL) {
ptr1[0] = 100;
ptr1[1] = 200;
printf("Custom Allocated Values: %d %d\n", ptr1[0], ptr1[1]);
} else {
printf("Allocation Failed.\n");
}
my_free(ptr1);
return 0;
}
🧠 Explanation:
This simple simulation shows how memory blocks can be marked used/free. Though it’s basic, this teaches core memory management—vital for low-level C interviews.
🎯 Final Words & Call-to-Action
You’ve just unlocked 12 carefully selected and real interview-level coding problems categorized into Basic, Intermediate, and Advanced levels—with easy explanations that make learning enjoyable and impactful.
But this is just the beginning.
👉 Want more such handpicked questions, but this time topic-wise, with optimized solutions, real interview guidance, and deep expert explanations?
🚀 Get Instant Access to Our Paid C Programming Interview Preparation Course
✅ Topic-wise Q&A
✅ Debugging Challenges
✅ Optimization Techniques
✅ Real-World Scenarios
✅ Code Breakdown + Insights
🔓 Unlock your edge in C Interviews – [Buy the Full Course Now]
