Top 50+ Programming Interview Questions and Answers in 2022

By | October 18, 2021
Programming Interview Questions

Are you preparing for an upcoming programming interview? Here we have compiled a list of general programming interview questions that will help you to prepare for the same. Every year, thousands of computer science graduates face technical interviews organized by IT companies for the role of senior and junior developers.

Programming interviews are designed to test the developer’s proficiency in data structures, algorithms, logic building, and problem-solving rather than just knowledge of a specific programming language.

Vamware

Top 50+ Programming Interview Questions

In the following section, we will discuss top programming interview questions and answers, which will give you a good idea of what type of software developer interview questions you can expect during an interview.

Basic Programming Interview Questions

Question No. 1: Name the most common errors that can occur during the execution of a program?

Answer:

  • Logical Error: If there is a mistake in the program source code, it could lead to a logical error. If there is a logical error in the program, then the output of the program could show unexpected behavior, or it could also crash the program. For instance, if we use if (x=1) instead of if (x==1), this could raise a logical error.
  • Runtime Error: This type of error occurs during the runtime, and thus, it is also known as a compile-time error. The logical error is an example of a runtime error.
  • Syntax Error: This error occurs when there is a mistake while writing code. For instance, a syntax error results when the developer forgets to put a semicolon at the end of the statement or close a bracket.

Question No. 2: What is the difference between a syntax error and a semantic error?

Answer: A syntax error occurs if there is a grammatical mistake in the code. This means that if the code does not follow the writing rules of the program, then there would be a syntax error. If there is a syntax error in the program, the code will not compile.

Syntax Error Example:

for(int i=0; i< 10; i++)
{ 
    cout<<i;
    cout<<endl;
// syntax error because there are no closing curly brackets

A semantic error occurs during the execution of the code and after the code compilation. If the program returns an unexpected output, this means there is a semantic error in the code. Incorrect variable types or sizes, non-existent variables, and subscripts out of range are some examples of semantic errors.

Semantic Error Example:

void fun (int a)

   {  a=b;  
   }
// We have not defined b

Question No. 3: How do you define an algorithm?

Answer: An algorithm is a finite set of steps or instructions that can be used to solve a problem. An algorithm should be clear, efficient, unambiguous, and finite.

Question No. 4: What is a low-level programming language?

Answer: Low-level programming does not require any translator, such as a compiler or interpreter, and its code can be directly executed by the system processor. Machine code (or binary code) and assembly code are examples of low-level programming.

Question No. 5: What is a high-level programming language?

Answer: A high-level programming language is a human-readable language and does require a translator so that the system processor can execute it.

Question No. 6: What are keywords?

Answer: Keywords are the reserved words used in programming languages that perform specific tasks. Each programming language has a set of reserved keywords. For instance, C++ has 64 keywords, Java has 51 keywords, and Python has 33 keywords.

Question No. 7: What are loops?

Answer: A loop is a basic structure present in every high-level programming language that is used to repeat a defined set of statements again and again till the condition becomes false. There are three major types of loops in programming:

1. for Loop

Example:

for(int i=0; i<10; i++)
{   
    cout<<i;
    cout<<endl;
}

2. while Loop

int i=0; 
while( i < 10) {
      cout <<i;
      cout<<endl;
      a++;
   }

3. do while Loop

int i = 0;
do {
  cout << i << "\n";
  i++;
}
while (i < 10);

Question No. 8: What is an array?

Answer: An array is a collection of similar elements where each element is stored sequentially. Arrays use indexing to access every element, and the indexing starts from 0 and goes up to n-1, where n is the total number of elements present in the array.

Question No. 9: What is debugging?

Answer: It is a process of removing all the errors and exceptions that occurred in the program during the compile time or runtime.

Question No. 10: What is the difference between break and continue statements?

Answer:

break continue
It ends or terminates the complete loop. It skips only one iteration.
Break takes execution out of the loop. Program execution remains in the loop until the loop condition becomes false.
It can be used when we have a specific condition from the loop and we do not want more iteration. The continue statement is used when we want to skip some specific conditions inside the loop.
It can be used with switch statements. Continue can not be used with switch statements.

Question No. 11: What are identifiers?

Answer: Identifiers are the names given by the programmer to program elements, such as variable names, class names, and function names. There are some specific sets of rules – depending on the chosen programming language – that the developers need to follow while naming a program element.

Question No. 12: Can we use a keyword as an identifier?

Answer: No, we cannot use a keyword as an identifier. If we try to do so, then the compiler will throw a syntax error.

Question No. 13: What are arithmetic operators, and on which data type can they operate?

Answer: Arithmetic operators are used to perform arithmetic operations on numeric data types, such as integers, float, and double.

Question No. 14: Where do we use the relational operators, and what value do they return?

Answer: Relational operators are used to compare two values, and they always return a Boolean value, i.e., either True or False.

Question No. 15: What is machine code?

Answer: A set of instructions that do not require any translator and is directly processed by the microprocessor is called the machine code. It is available in binary form and considered low-level code.

 

Intermediate Programming Interview Questions

Question No. 16: What will happen if we use a break statement inside a nested loop?

Answer: If we use the break statement inside a nested loop, then the break statement will bring program execution only out from that loop where it is initialized. For instance, suppose we have a child loop inside a parent loop and there is a break statement inside the child loop.

Then, when the compiler executes the break statement, the program execution will be thrown out from the child loop, but we still remain in the parent loop. Thus, the outer loop will not be affected by the break statement if the break statement is inside the inner loop.

Question No. 17: What is a data structure?

Answer: Data structure refers to a way or technique used to organize collective data in a specific manner. There are various data structures in the programming world, and each has its significance and operations.

Question No. 18: What are linear and non-linear data structures?

Answer: If all the elements in a data structure are organized sequentially, then it would be considered as a linear data structure. Arrays, linked lists, and queues are examples of linear data structures.

Linear Data Structure

In a non-linear data structure, all the elements are not organized sequentially. Graphs and trees are examples of non-linear data structures.

Non- Linear Data Structure

Question No. 19: When do we use for loop, and when do we use the while loop?

Answer: If we are sure about the number of times we want to execute a particular set of code, then we use the for loop. However, if the number of execution of the code is not certain, then we use the while loop.

Question No. 20: Why use the do while loop if we have the while loop?

Answer: When we want to execute a set of code at least one time, even the condition is false at the initial state, then we use the do while loop.

Question No. 21: Even if the condition is false at the initial state, do while executes once, but while does not. Why?

Answer: The do while loop first executes the code, then checks the condition for subsequent iterations. However, the while loop first checks the condition, then executes the code. That’s why if the condition is false at the initial state, the while loop will not execute while the do while loop will.

Question No. 22: What is a compiler?

Answer: A compiler is a computer program that acts as a translator, and its main job is to convert code from one programming language to another. In programming terms, the compiler converts high-level programming language code to low-level code, such as byte code and machine code.

Question No. 23: What is a subroutine?

Answer: A subroutine is a sequential set of program instructions and is used to perform a specific task. In programming languages, we define subroutines as procedures, functions, methods, or a subprogram. We can define a subroutine in the program or separately in a library.

Question No. 24: What are modeling languages?

Answer: It is a made-up language that is used to represent a piece of information, algorithm, or knowledge of a structure. We can either use graphical or textual notation to represent a modeling language. The flowchart is a perfect example of modeling languages.

Question No. 25: What is a flowchart?

Answer: A flowchart is a graphical and pictorial representation of the program control flow. Also, it is one of the modeling languages that are used to describe the algorithm of a particular program or syntax.

Example:

Flow Chart Example

Question No. 26: Give some examples of modeling languages.

Answer:

  • Business Process Modeling Notation
  • EXPRESS
  • Extended Enterprise Modeling Language
  • Flowchart
  • Fundamental Modeling Concepts
  • Jackson Structured Programming
  • Unified Modeling Language
  • Alloy (specification language
  • Systems Modeling Language

Question No. 27: What is maintaining and updating a program?

Answer: After the process of program planning, development, testing, and deployment, the maintenance of the program begins. In this stage, we upgrade the program with new functionalities (updating a program) and try to improve the program with improved features and eliminate the existing issues.

Question No. 28: What is a library?

Answer: A library is a collection of files that contain precompiled code, such as routines, functions, methods, and classes. Every high-level programming language has many open-source libraries that can be installed from the internet and plugged into our code.

Question No. 29: What do you understand by documentation?

Answer: Documentation is the descriptive guide of a program, algorithm, library, programming language, framework, coding method, testing, and so on. In simple terms, documentation of a project is a written report that provides all the public information and operation of the same.

Question No. 30: What is the beta version of the software?

Answer: A beta version generally refers to an unstable version of the software, which is still in the testing and up-gradation process. Companies release beta versions so they can get feedback from their customers on the release of its new features and change it accordingly.

Question No. 31: How does a program execute?

Answer: The computer understands only machine language, so before the execution, the compiler converts the human-written code into machine language. Next, the operating system transfers that machine code from the secondary memory to the primary memory (RAM) because the processors that execute the instructions are linked to the main memory.

Question No. 32: Why do we use comments in a program?

Answer: Comments specify additional information about the code. The compiler and interpreter do not read the comments, and that’s why they do not get compiled or executed when we run a program.

 

Advanced Programming Interview Questions

Question No. 33: What do you understand by the DRY principle?

Answer: DRY stands for Don’t Repeat Yourself, and there are many programming languages, libraries, and frameworks that work on this principle. The principle states that there is no need to repeat code in a program. Write it once, and use it anywhere throughout the program. The DRY principle increases code reusability and eliminates repetition.

Question No. 34: What are constants in programming, and how many types of constants are there?

Answer: A value that can neither be changed during the execution of a program nor by the computer itself is known as a constant. For instance, character ‘B’ is a constant value with an ASCII code 66. There are two types of constants:

1. Numeric Constants

Any digit or a sequence of digits in programming is a numeric constant, and there are different types of numeric constants, such as Decimal and Octal.

Example: 

a= 10   // here a is variable and 10 is numeric constant.

2. Character Constants

Any alphanumeric character, which is inside a single or double quotation, is a character constant. If there is a sequence of characters, it would be a string character constant.

Example:

chr = 'a'   // here chr is variable and 'a' is charactere constant.
string = "Hello" // string is variable and "Hello" is string constant

Question No. 35: What is software testing?

Answer: Software testing is a process in which the developed software goes through some testing criteria before it gets deployed. While testing software, many criteria test the reliability, efficiency, and interactivity of the software.

Question No. 36: What is bubble sort, and what is its time complexity?

Answer: Bubble sort is a sorting algorithm in which we repeatedly swap the adjacent elements if they are in the wrong order. The time complexity of bubble sort is O(n2).

Question No. 37: List the significant sorting algorithms with their time complexities.

Answer:

Algorithms Time Complexity
Best Average Worst
Selection Sort Ω(n2) θ(n2) O(n2)
Bubble Sort Ω(n) θ(n2) O(n2)
Insertion Sort Ω(n) θ(n2) O(n2)
Heap Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
Quick Sort Ω(n log(n)) θ(n log(n)) O(n2)
Merge Sort Ω(n log(n)) θ(n log(n)) O(n log(n))
Bucket Sort Ω(n+k) θ(n+k) O(n2)
Radix Sort Ω(nk) θ(nk) O(nk)

Question No. 38: Write a function that can count the occurrence of a given character in a string.

Answer:

int count_character(string s, char chr)
{
    // Count variable
    int count = 0;
    for (int i=0;i<s.length();i++)
        // checking character in string
        if (s[i] == chr)
            count++;
    return count;
}

Question No. 39: Write an algorithm to implement counting sort.

Answer:

Counting_Sort(array, size)    // counting sort function, which accepts an array and its size
maxi <- max(array)   //the largest element from the array
count[size]   // initializes an array count whose all elements are zero

for j 0 to size
            find the total count of each unique element and store the count at jth index in count array
for i 0 to maxi
            find the cumulative sum and store it in count array itself
for j <- size down to 1
            restore the elements to array decrease count of each element restored by 1

Question No. 40: Write a logic that can remove duplicate elements from a sorted array. The time complexity of the program should be O(N), where N is the total number of elements present in the array.

Answer:

if (n==0 || n==1) // check for the base case if the size of array is 1 or 0
        return n;
 else
 {
     int temp[n];   // create a temporary array temp which size is similar to original array array
    // Start traversing elements
    int j = 0;
    for (int i=0; i<n-1; i++)
         // this If statement make sure that only unique elements of the array arr
         // store in temporary array temp
        if (arr[i] != arr[i+1])
            temp[j++] = arr[i];

    // Store the last element as whether
    // it is unique or repeated, it hasn't
    // stored previously
    temp[j++] = arr[n-1];

    // Modify original array
    for (int i=0; i<j; i++)
        arr[i] = temp[i];
    }

Question No. 41: Write a function that can reverse an array in place, and the time complexity of the program should be O(N).

Answer:

 void Array_Reverse(int arr[], int front, int back)

{
    int temp;
    while (front < back)
    {
        // swapping the back element of the array with the front elements
       //with each iteration till the front < back
        temp = arr[front]; 
        arr[front] = arr[back];
        arr[back] = temp;
        front++;
        back--;
    } 
}

Question No. 42: How will you swap two numbers without using a third variable?

Answer:

int a, b;
a= 30;
b=40;


a= a+b;   // now a = 70
b= a-b;   // now b =30
a= a-b;    // now a =40

Question No. 43: What is the difference between stable and unstable sorting algorithms?

Answer: A sorting algorithm will be termed as a stable sorting algorithm if the algorithm maintains the relative order of elements even after the sorting. But if the algorithm changes the relative order of the elements, then it would be considered as an unstable sorting algorithm.

Question No. 44: Add two numbers without using the + operator.

Answer:

int Addition(int a, int b) 
{ 
    while (b != 0) 
    { 
        int carry = a & b; 
        a = a ^ b; 
        b = carry << 1; 
    } 
    return a; 
}

Question No. 45: How do you check if two rectangles overlap with each other?

Answer: We can represent a rectangle by two coordinates; top left(ln) and bottom right(rn).

Rectangle 1 with coordinates point l1, and r2.

Rectangle 2 with coordinates point l2 and r2.

bool Overlap(Point l1, Point r1, Point l2, Point r2)
{
    // If one rectangle is on left side of other
    if (l1.x >= r2.x || l2.x >= r1.x)
        return false;
    // If one rectangle is above other
    if (l1.y <= r2.y || l2.y <= r1.y)
        return false;
    return true;
}

Question No. 46: Differentiate between linked lists and arrays.

Answer:

Array Linked List
The array contains elements of the same data type. A linked list contains heterogeneous elements.
Arrays use indexing, which makes it easy to access every element. Linked lists do not have indexing to access the elements. We have to traverse from the initial node.
Insertion and deletion are costly operations in an array. It is very easy to insert and remove nodes from a linked list.
An array occupies contiguous memory. The nodes of a linked list occupy random free memory locations.
An array comes with a fixed size. The linked list is very flexible, and its size can be increased and decreased.

Question No. 47: What is the efficient way to insert a new node in the middle of a linked list?

Answer: We can use the tortoise and hare algorithm, in which we use two pointers. One is fast, and the other is slow. The quick pointer moves two steps at a time while the slow pointer moves one, so at the time the quick tip is at the end of the linked list, the slow pointer would be in the middle, and using the slow pointer, we will insert the new node in the linked list.

Question No. 48: Use a programming language and print the reverse of a string using recursion.

Answer:

#python
def reverse(string):
    if len(string) == 0:
        return
    temp = string[0]
    reverse(string[1:])  #recursion
    print(temp, end='')

string = "TechGeekBuzz"
reverse(string)

Question No. 49: Create a function that could tell whether the given linked list contains a cycle or not.

Answer: Traverse through the list nodes one by one and keep putting the address of each node in a data structure. While traversing through the list, if you hit a node that points to a NULL or NONE value, then return False. Else, if you run a node that points to a visited node whose address is present in the data structure, then return True.

Python Code:

def Contain_Cycle(head):
    ds = set()
    temp = head
    while (temp):
    
        if (temp in ds): #if the node is present in the data structure
            return True

        ds.add(temp)
        temp = temp.next
    return False

Question No. 50: What are the major points we need to take care of when we implement a BST (Binary Search Tree)?

Answer:

  • The left subtree key node value must be less than its parent node.
  • The right subtree key node value must be higher than its parent node.
  • Each one of the left and right subtrees must also be a binary search tree.
  • There must be no duplicate nodes.

Question No. 51: What do you understand by the binary tree traversal?

Answer: Unlike other data structures, we have various methods to traverse through binary tree nodes. There are three major traversal algorithms we can follow to traverse through each node of a binary tree:

  1. inorder traversal (Left Root Right)
  2. preorder traversal (Root Left-Right)
  3. postorder traversal (Left-Right Root)

Question No. 52: Give the algorithm for inorder traversal.

Answer:

  • First, traverse the left subtree (recursively).
  • Next, visit and print the root node.
  • Finally, traverse through the right subtree (recursively).

Question No. 53: What is the algorithm for inorder tree traversal without recursion?

Answer:

  • Create stack S.
  • Create a temp node from the root node, such as temp = root.
  • Push the temp node to the stack S and change the temp node with the left node of the temp, such as temp = temp-> left_node_of_temp
  • Set an IF statement. If temp is NULL or NONE and stack S is not empty, then:
    • Pop the item from the stack.
    • Print the pop item and set the temp node as the popped right node, such as temp = popped_item->right.
    • Now push the temp node to the stack S and change the temp node with the left node of the temp such as temp = temp-> left_node_of_temp.
  • If temp is NULL and the stack is empty, then we are done.

Conclusion

That sums up our list of the top programming interview questions and answers. We hope you go through these questions first before you go for the interview. During the interview, you will be tested more for necessary programming skills than your advanced knowledge in a specific programming language. So you should have a good understanding of programming, data structures, and algorithms.

People are also reading:

Leave a Reply

Your email address will not be published. Required fields are marked *