Skip to content
HackerRank Launches Two New Products: SkillUp and Engage Read now
Join us at the AI Skills & Tech Talent Summit in London! Register now
The 2024 Developer Skills Report is here! Read now
Stream HackerRank AI Day, featuring new innovations and industry thought leaders. Watch now
Programming Languages

7 C Interview Questions Every Developer Should Know

Written By April Bohnert | July 17, 2023

The programming language C stands as a key pillar in the world of computer science, remaining valuable even as the tech industry continues to evolve. Its flexibility, efficiency, and its influenceon many other languages make it a foundational skill for many developers, and a language of choice for system programming, embedded systems, and more

Having a firm grasp of C can open doors to numerous opportunities. It’s a language that often acts as a stepping stone to others, giving developers a more in-depth understanding of how computers really work. For instance, it provides first-hand experience with concepts like memory management, pointers, and low-level access to system resources that are abstracted away in many high-level languages. This proficiency in C can significantly improve programming capabilities overall, resulting in better code efficiency and optimization in whichever language you work in next.

But for those aspiring to ace a C programming interview, it’s essential to move beyond the basics. One must challenge their coding abilities and practice working on the types of problems likely to be encountered in an interview setting. Here, we’ll discuss the details of the C language, explain what a typical C interview looks like, and provide a series of challenging C interview questions to help you sharpen your skills and stand out. 

What is C?

Created in the early 1970s, C is a general-purpose language with features that allow developers to build programs operating directly on a physical machine’s level. Due to its low-level capabilities, C has been instrumental in writing system software, enabling the development of compilers, interpreters, operating systems, and database systems.

C’s syntax is simple, making the language easy to learn and use. However, don’t let the simplicity fool you. C’s low-level nature and access to memory management, combined with its simplicity, make it an efficient language for creating fast, compact programs.

A distinct feature of C is its ability to manipulate system hardware directly through the use of pointers. This characteristic makes it invaluable for applications that require high performance or that operate at a system level, such as creating desktop applications, game development, and implementing other programming languages.

What a C Programming Interview Looks Like

A C programming interview often involves a range of elements. While the specific format may vary from company to company, you can generally expect a combination of coding exercises, problem-solving questions, and conceptual discussions. 

In coding exercises, you’ll be tasked to write C programs or functions that solve a specific problem or perform a particular task. These challenges can range from data structure and algorithm problems to system-level tasks, reflecting the range of applications where C shines. It’s also not uncommon to face questions involving memory management or pointers, given their significance in C.

The problem-solving questions are aimed at assessing your logical thinking and approach to software design. You might be presented with a high-level problem and asked to outline how you’d tackle it using C. This could involve discussing the data structures you’d use, the algorithms you’d implement, and how you’d ensure your solution is efficient and robust.

Conceptual discussions focus on your understanding of C and its various constructs. You might be asked about how certain features of C work, like its memory management system, how pointers function, or the purpose of specific keywords. It’s crucial to not only know how to use C but to understand how it works behind the scenes.

C Interview Questions

In this section, we’ll dive into a series of C interview questions, starting from an intermediate level and getting progressively tougher. Remember, the goal isn’t to breeze through these questions, but to stretch your C programming skills and prepare you for the types of challenges you might encounter in a real-life interview.

1. Array Rotation

Task: Write a C function named ‘rotate’ that takes an array and its length as input and rotates the array in-place to the right by one position.

Input Format: The input will be an integer array and its length.

Constraints: The array length will be at least 1.

Sample Input: int arr[] = {1, 2, 3, 4, 5}; int n = 5;

Array after rotation: 5 1 2 3 4

Sample Code:

void rotate(int arr[], int n) {

   int temp = arr[n-1], i;

   for (i = n-1; i > 0; i--)

      arr[i] = arr[i-1];

   arr[0] = temp;

}

Explanation: 

The ‘rotate’ function temporarily stores the last array element to prevent data loss during the shifts. Next, using a for loop, it moves all the elements one step to the right. Finally, it places the saved element at the array’s start, completing the rotation.

This question tests the candidate’s ability to work with arrays and manipulate them, a fundamental skill in C programming. It mimics real-world scenarios where developers often need to shift, rotate or rearrange data stored in arrays. 

2. String Palindrome

Task: Write a C function named ‘isPalindrome’ that checks whether a given string is a palindrome or not.

Input Format: The input will be a string.

Constraints: The string will only contain alphanumeric characters.

Output Format: The function should return 1 if the string is a palindrome and 0 otherwise.

Sample Input: racecar

Sample Output: 1

Sample Code:

int isPalindrome(char* str) {

    int l = 0;

    int h = strlen(str) - 1;

 

    while (h > l)

    {

        if (str[l++] != str[h--])

        {

            return 0;

        }

    }

    return 1;

}

Explanation:

The ‘isPalindrome’ function first finds the length of the string and sets two pointers, one at the start (l) and one at the end (h). It then compares the characters at the positions pointed by ‘l’ and ‘h’. If they are not the same, it returns 0, indicating that the string is not a palindrome. If they are the same, it increments ‘l’ and decrements ‘h’ and repeats the comparison until ‘h’ is less than ‘l’, at which point it returns 1, confirming that the string is a palindrome.

This question tests a candidate’s understanding of string manipulation and comparison, fundamental tasks in many real-world applications. Well-crafted solutions demonstrate the candidate’s proficiency with string operations and working with pointers in C.

3. Linked List Reversal

Task: Write a C function called ‘reverseList’ that reverses a linked list.

Input Format: The input will be a pointer to the head of a singly linked list.

Sample Input: 1 -> 2 -> 3 -> 4 -> NULL

Linked list after rotation: 4 -> 3 -> 2 -> 1 -> NULL

Sample Code:

typedef struct Node {

    int data;

    struct Node* next;

} Node;




void reverseList(Node** head_ref) {

    Node* prev   = NULL;

    Node* current = *head_ref;

    Node* next = NULL;

    while (current != NULL) {

        next  = current->next;  

        current->next = prev;   

        prev = current;

        current = next;

    }

    *head_ref = prev;

}

Explanation:

The ‘reverseList’ function reverses a linked list by reassigning the ‘next’ pointers of each node. It iterates through the linked list, starting at the head, and in each iteration, it reassigns the ‘next’ pointer of the current node to the previous node. After each iteration, it moves to the next node by following the original ‘next’ pointer, which it saved before reassignment. When it reaches the end of the list, it assigns the ‘prev’ node as the new head of the linked list.

This question tests a candidate’s proficiency with pointers and dynamic memory allocation, and their understanding of complex data structures like linked lists, skills often necessary when working with C in system-level programming.

Explore verified tech roles & skills.

The definitive directory of tech roles, backed by machine learning and skills intelligence.

Explore all roles

4. Calculate the Nth Term

This challenge will help you test your understanding of recursion.

A function that calls itself is known as a recursive function. The C programming language supports recursion. But while using recursion, one needs to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.

To prevent infinite recursion, an ‘if…else’ statement (or a similar approach) can be used where one branch makes the recursive call and the other doesn’t.

void recurse() {

    .....

    recurse()  //recursive call

    .....

}

int main() {

    .....

    recurse(); //function call

    .....

}

Task: There is a series, ‘S’, where the next term is the sum of previous three terms. Given the first three terms of the series, ‘a’, ‘b’, and ‘c’ respectively, you have to output the nth term of the series using recursion.

Input Format: The first line contains a single integer, ‘n’. The next line contains 3 space-separated integers, ‘a’, ‘b’, and ‘c’.

Constraints:

  • 1 ≤ n ≤ 20
  • 1 ≤ a,b,c ≤ 100

Output Format: Print the nth term of the series, S(n).

Sample Input:

5

1 2 3

Sample Output: 11

Explanation:

Consider the following steps:

  1. S(1) = 1
  2. S(2) = 2
  3. S(3) = 3
  4. S(4) = S(3) + S(2) + S(1)
  5. S(5) = S(4) + S(3) + S(2)

From steps 1, 2, 3, and 4, we can say S(4) = 3 + 2 + 1 = 6; then using the values from step 2, 3, 4, and 5, we get S(5) = 6 + 3 + 2 = 11. Thus, we print 11 as our answer.

Solve the problem.

5. Implementing a Stack

Task: Write a C program to implement a stack using a linked list. Your program should have ‘push’ and ‘pop’ operations.

Input Format: The inputs will be the elements to be pushed to the stack.

Output Format: The outputs will be the elements popped from the stack.

Sample Input: push(1), push(2), push(3), pop(), pop()

Sample Output: 3, 2

Sample Code:

typedef struct Node {

    int data;

    struct Node* next;

} Node;




Node* top = NULL;




void push(int data) {

    Node* newNode = (Node*)malloc(sizeof(Node));

    if (!newNode) {

        printf("Heap overflow");

        exit(1);

    }

    newNode->data = data;

    newNode->next = top;

    top = newNode;

}




int pop() {

    Node* temp;

    int poppedValue;




    if (top == NULL) {

        printf("Stack underflow");

        exit(1);

    }

    else {

        temp = top;

        top = top->next;

        poppedValue = temp->data;

        free(temp);

        return poppedValue;

    }

}

Explanation:

The given code demonstrates the implementation of a stack data structure in C using a singly linked list. The ‘push’ function creates a new node, checks if memory allocation was successful, and if so, assigns the input data to the new node and adds it to the top of the stack. The ‘pop’ function checks if the stack is empty and if it isn’t, removes the topmost node from the stack, returns its value, and frees the memory allocated to it.

This question tests a candidate’s understanding of fundamental data structures (like stacks) and their implementation in C, along with the use of dynamic memory allocation. Successfully tackling this problem demonstrates proficiency in handling memory, managing pointers, and understanding the lifecycles and behaviors of data structures, which are crucial for low-level and system programming tasks in C.

6. Binary Tree Traversal

Task: Write a C function named ‘inorderTraversal’ that performs an in-order traversal of a binary tree.

Input Format: The input will be a pointer to the root node of a binary tree.

Output Format: The function should print the nodes of the binary tree in an in-order traversal sequence.

Sample Input: Binary tree with nodes arranged as follows: 1 (root), 2 (left child of 1), 3 (right child of 1)

Sample Output: 2, 1, 3

Sample Code:

typedef struct Node {

    int data;

    struct Node* left;

    struct Node* right;

} Node;




void inorderTraversal(Node* root) {

    if (root != NULL) {

        inorderTraversal(root->left);

        printf("%d ", root->data);

        inorderTraversal(root->right);

    }

}

Explanation:

The ‘inorderTraversal’ function uses recursion to traverse a binary tree in in-order fashion, which means visiting the left subtree, the root node, and then the right subtree. If the given root node is not null, the function recursively calls itself to visit the left subtree, then it prints the root’s data, and finally, it calls itself again to visit the right subtree.

This question tests a candidate’s understanding of binary trees and their traversals, along with the concept of recursion. It’s a commonly asked question in C interviews because these are fundamental concepts often utilized in many computer science problems. 

7. Hash Table Implementation

Task: Write a C program to implement a simple hash table. This hash table will use chaining to handle collisions.

Input Format: The input will be the keys and values to be inserted in the hash table.

Output Format: The program should be able to retrieve values given the keys.

Sample Code:

#include <stdlib.h>

#include <string.h>




typedef struct node {

    char* key;

    char* value;

    struct node* next;

} node;




typedef struct hashtable {

    int size;

    struct node** table;

} hashtable;




hashtable* createHashtable(int size) {

    hashtable* newTable;

    if (size < 1) return NULL;

    if ((newTable = malloc(sizeof(hashtable))) == NULL) {

        return NULL;

    }

    if ((newTable->table = malloc(sizeof(node*) * size)) == NULL) {

        return NULL;

    }

    int i;

    for (i = 0; i < size; i++) {

        newTable->table[i] = NULL;

    }

    newTable->size = size;

    return newTable;

}




unsigned int hash(hashtable* hashtable, char* key) {

    unsigned int hashval = 0;

    for (; *key != '\0'; key++) {

        hashval = *key + (hashval << 5) - hashval;

    }

    return hashval % hashtable->size;

}




node* createNode(char* key, char* value) {

    node* newNode;

    if ((newNode = malloc(sizeof(node))) == NULL) {

        return NULL;

    }

    newNode->key = strdup(key);

    newNode->value = strdup(value);

    newNode->next = NULL;

    return newNode;

}




void set(hashtable* hashtable, char* key, char* value) {

    unsigned int bin = hash(hashtable, key);

    node* newNode = createNode(key, value);

    newNode->next = hashtable->table[bin];

    hashtable->table[bin] = newNode;

}




char* get(hashtable* hashtable, char* key) {

    unsigned int bin = hash(hashtable, key);

    node* pair = hashtable->table[bin];

    while (pair != NULL && pair->key != NULL) {

        if (strcmp(pair->key, key) == 0) {

            return pair->value;

        }

        pair = pair->next;

    }

    return NULL;

}

Explanation:

The provided code demonstrates the implementation of a simple hash table with chaining in C. It includes the creation of a hash table, generation of hash values from keys, and insertion and retrieval of key-value pairs. The hash table uses a simple hash function to convert keys into indices. If collisions occur (when different keys result in the same index), chaining is used by maintaining a linked list of key-value pairs for each index in the hash table.

This question is challenging because it involves various crucial concepts in C: dynamic memory allocation, pointers, linked lists, and data structures. Hash tables are widely used due to their efficiency in key-value pair storage and retrieval. 

Resources to Improve C Knowledge

This article was written with the help of AI. Can you tell which parts?

Abstract, futuristic image generated by AI

6 REST API Interview Questions Every Developer Should Know