Programming Interview Question
Programming Interview Question
Logical errors – This occurs in the scenario of a computer program implementing the
wrong logic. As there is no report generated for these types of programming errors,
they are the most difficult ones to deal with.
Runtime errors – Occurs when the program contains an illegal operation. For example,
dividing a number by 0. These are the only errors that are displayed instantly during the
program execution. Upon the occurrence of a runtime error, the program execution is
stopped and a diagnostic message is displayed.
Syntax errors – Occurs when one or more grammatical rules of the programming
language being used is violated. Such errors are detected during compile time.
Question: Please explain an algorithm. What are some of its important features?
Answer: An algorithm can be defined as a set of finite steps that when followed helps in
accomplishing a particular task. Important features of an algorithm are clarity, efficiency, and
finiteness.
Question: What do you understand by maintaining and updating a computer program?
Answer: The maintenance and updating process of a computer program starts post its
successful installation. While program maintenance is the continuous process of monitoring the
computer program for bugs and errors, updating the computer program means making it better
with minor and major changes over time.
Question: Please provide a brief explanation on variables.
Answer: Variables are used for storing the input of a program as well as the computational
results during program execution. These are actually named memory locations. The value
stored in a variable can change during the program execution.
Question: Every programming language has reserved words. What are they? Give some
examples.
Answer: Reserved words, also known as keywords, are the words that have predefined
meanings in a particular programming language. These reserved words can’t be used or
redefined for serving other purposes. Following are some examples of reserved words:
Question: What do you understand by loops? Briefly explain the various types of loops.
Answer: A loop is a structure in programming that can repeat a defined set of statements for a
set number of times or until a particular condition is satisfied. There are three important types
of loops:
FOR…NEXT Loop – This is the most effective loop when you know beforehand the total
number of times the loop is to be repeated
WHILE…WEND Loop – It keeps on repeating a particular action until the concerned
condition becomes false. This loop is particularly useful when the total number of
repetitions is unknown.
Nested Loop – When a loop is used inside a loop then it is termed as a nested loop
Artificial intelligence
Compiler design
Database management
Graphics
Numerical analysis
Operating systems
Statistical analysis
Question: What do you understand by sorting? Name some popular sorting techniques.
Answer: Sorting is the process of arranging the elements of an array in either ascending or
descending order. Some of the popular sorting techniques are:
Bubble sort
Heap sort
Insertion sort
Merge sort
Quick sort
Selection sort
Question: Please explain the binary search. What is the best scenario for using a binary
search?
Answer: The binary search is best applied to a list in which all the elements are already sorted.
The binary search starts with searching in the middle of the list.
If the middle element is not the targeted element then it proceeds to search either the lower
half or the upper half of the list. The process keeps on repeating until the desired element is
found.
Question: How will you reference all elements in a one-dimensional array?
Answer: We need to use an indexed loop for referencing all elements in a one-dimension array.
The counter starts from 0 to the number equal to 1 less than the array size. Hence, all elements
are referenced in sequence by employing the loop counter as the array subscript.
Question: What do you understand by LIFO and FIFO?
Answer: LIFO and FIFO are two of the most popular forms of accessing, retrieving, and storing
data. LIFO stands for Last In First Out. This means that in this approach the latest stored data is
retrieved the first. This approach is followed in a stack.
FIFO stands for First In First Out and is the opposite approach of LIFO. Here, the data that is
stored the oldest is the one to be retrieved first. FIFO approach is followed in a queue.
Question: Can you explain multi-dimensional array?
Answer: A conventional array has only one index. A multi-dimensional array is one that has
multiple indexes. It is used where single-dimensional indexing is insufficient.
Question: Please explain a graph.
Answer: A type of data structure that has a set of ordered pairs is called a graph. These ordered
pairs are also called arcs or edges. They are used for connecting nodes, which is where data is
stored to and retrieved from.
Question: What is the difference between a linear and a non-linear data structure?
Answer: In a linear data structure, data elements are placed adjacent to each other. Arrays,
linked lists, queues, and stacks are some examples of linear data structure.
In a non-linear data structure, it is possible for data elements to be connected to more than
two data elements. Examples of the non-linear data structure are graphs and trees.
Question: Please explain an AVL tree.
Answer: A binary search tree that is always partially balanced is called an AVL tree. It is the first-
ever data structure to be designed in such a way. The balance is the difference between the
heights of the subtrees from the root.
Question: Why do we use Huffman’s algorithm?
Answer: For extending binary trees that have minimum weighted path length from given
weights, we use Huffman’s algorithm. It uses a table containing the total number of times for
each data element.
Question: Please explain Fibonacci search.
Answer: Fibonacci search is a type of search algorithm that applies to a sorted array. It uses the
divide-and-conquer approach for greatly reducing the time required for reaching the target
element.
Question: How does the recursive algorithm works?
Answer: The recursive algorithm divides a problem into smaller, easy-to-manage sub-problems.
The output gained from one recursion after processing one sub-problem becomes input for the
subsequent recursive process.
Question: What is a recursive function?
Answer: A function that calls itself is called a recursive function. It is based on a terminating
condition and uses a stack. The phenomenon is called recursion.
Question: Please explain how does dynamic memory allocation help in managing data?
Answer: Dynamic memory allocation helps in storing simple structured data types. Moreover, it
helps in combining separately allocated structure blocks for forming composite structures that
can be expanded or contracted as required.
Question: What is the difference between NULL and VOID?
Answer: While NULL represents a value, VOID represents data type identifier. A variable with a
NULL value represents an empty value. Pointers that have no initial size are identified using
VOID.
Question: Please explain how variable declaration affects memory allocation.
Answer: A particular data type is defined with a variable declaration. The total amount of
memory to be allocated depends on the data type a declared variable belongs to.
Question: Please explain data abstraction.
Answer: Data abstraction helps in breaking down a complex data problem into easily-
manageable sub-problems. Following data abstraction, first data objects and operations to be
performed on the same are specified. How the data objects will be stored in the memory
becomes a secondary task.
Question: What the code to check a String is palindrome or not?
Answer:
#include <string.h>
int main()
{
char a[1000];
int i,n,b=0;
printf("Enter the string : ");
gets(a);
n=strlen(a);
for(i=0;i<n/2;i++)
{
if(s[i]==a[n-i-1])
b++;
}
if(b==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}
Output:
Question: Write a method that will remove any given character from a String?
Answer:
Result:
Question: Write a function to find out the longest palindrome in a given string?
Answer:
Result Output:
import java.io.*;
import java.util.*;
class DupElements
{
static int duplicate(int[] arr, int n)
{
// Find array sum and subtract sum
// first n-1 natural numbers from it
// to find the result.
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum - (((n - 1) * n) / 2);
}
// Driver code
public static void main(String args[])
{
int[] arr = { 9, 8, 2, 6, 11, 8, 18, 3, 4, 5 };
int n = arr.length;
System.out.println(duplicate(arr, n));
}
}
Output:
class NodeTree {
int data;
Node left, right;
Node(int d)
{
data = d;
left = right = null;
}
}
class BinaryTree {
NodeTree root;
/* Returns true if binary tree with root as root is height-balanced */
boolean isBalanced(NodeTree node)
{
int lh; /* for height of left subtree */
int rh; /* for height of right subtree */
/* If tree is empty then return true */
if (node == null)
return true;
/* Get the height of left and right sub trees */
lh = height(node.left);
rh = height(node.right);
if (Math.abs(lh - rh) <= 1
&& isBalanced(node.left)
&& isBalanced(node.right))
return true;
/* If we reach here then tree is not height-balanced */
return false;
}
int height(Node node)
{
/* base case tree is empty */
if (node == null)
return 0;
/* If tree is not empty then height = 1 + max of left
height and right heights */
return 1 + Math.max(height(node.left), height(node.right));
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.left.left.left = new Node(8);
if (tree.isBalanced(tree.root))
System.out.println("Tree is balanced");
else
System.out.println("Tree is not balanced");
}
}
Output:
Output:
Question: Write a program to find out if two rectangles R1 and R2 are overlapping?
Answer:
class OverlapRect {
static class RectPoint {
int x, y;
}
// Returns true if two rectangles (a1, b1) and (a2, b2) overlap
static boolean doOverlap(Point a1, Point b1, Point a2, Point b2) {
// If one rectangle is on left side of other
if (a1.x > b2.x || a2.x > b1.x) {
return false;
}
// If one rectangle is above other
if (a1.y < b2.y || a2.y < b1.y) {
return false;
}
return true;
}
public static void main(String[] args) {
Point a1 = new Point(),
b1 = new Point(),
a2 = new Point(),
b2 = new Point();
a1.x=0;a1.y=10; b1.x=10;b1.y=0;
a2.x=5;a2.y=5; b2.x=15;b2.y=0;
if (doOverlap(a1, b1, a2, b2)) {
System.out.println("Rectangles Overlap");
} else {
System.out.println("Rectangles Don't Overlap");
}
}
}
Output:
Rectangles Overlap
Question: Write a program for word-wrap which should work on any screen size?
Answer:
Output:
Output:
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
class Anagram {
static boolean areAnagram(char[] str1, char[] str2)
{
// Get lenghts of both strings
int n1 = str1.length;
int n2 = str2.length;
// If length of both strings is not same,
// then they cannot be anagram
if (n1 != n2)
return false;
// Sort both strings
Arrays.sort(str1);
Arrays.sort(str2);
// Compare sorted strings
for (int i = 0; i < n1; i++)
if (str1[i] != str2[i])
return false;
return true;
}
public static void main(String args[])
{
char str1[] = { 'w', 'e', 's', 't' };
char str2[] = { 't', 't', 'e', 'w' };
if (areAnagram(str1, str2))
System.out.println("The two strings are"
+ " anagram of each other");
else
System.out.println("The two strings are not"
+ " anagram of each other");
}
}
Output:
Question: Given two arrays, 1,2,3,4,5 and 2,3,1,0,5 find which number is not present in the
second array?
Answer:
class ABC
{
static void findMissing(int a[], int b[],
int n, int m)
{
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < m; j++)
if (a[i] == b[j])
break;
if (j == m)
System.out.print(a[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int a[] = { 1, 2, 3, 4, 5 };
int b[] = { 2, 5, 3, 1, 0 };
int n = a.length;
int m = b.length;
findMissing(a, b, n, m);
}
}
Output:
4, 0
Output:
Largest Number is : 87
Smallest Number is : 13
Output:
First Max Number: 50
Second Max Number: 34
Conclusion
Wanna prepare more? This highly rated udemy course has helped thousands of interview
candidates so must be useful to you as well: Break Away: Programming And Coding Interviews.
That completes our list of the programming interview questions that every aspiring
programmer must know, and these questions will help you to crack the technical interviews. If
you find some other problems at the time of the interview which are not listed here, please
comment, and we will add those in the given list. All the best!
People are also reading:
What is Programming?
What is Programming Languages?
Best Programming Books
Best Programming Languages to learn
Programming Languages for Getting a Jobs
What is Functional Programming?
What is Procedural Programming?
Programming Paradigm
How to learn to program?
How to learn Programming?
Free Coding Bootcamp
10 Best Web Development IDE
Share:
Vijay Singh
My name is Vijay Singh Khatri, and I enjoy meeting new people and finding ways to help them
have an uplifting experience. I have had a variety of customer service opportunities, through
which I was able to have fewer returned products and increased repeat customers, when
compared with co-workers. Currently working with hackr.io View all posts by the Author
Related Posts
10 Top Software Development Tools You Should Use In 2021
Read More
Name*
Comment*
SUBMIT
× Close Ad
Blog
Roadmaps
About Us
Programming Tips
Help & FAQ
We Feedback
Disclosure: This page may contain affliate links, meaning when you click the links and make a
purchase, we receive a commission.