Top Coding Interview Questions on Arrays-CThis tutorial lists the frequently asked coding questions in Interviews. All the questions are coded with the believed-to-be-best codes in the C programming language. All the questions are collected from different resources on the internet. First, an Array is a homogeneous and static Data Structure that stores elements in contiguous memory locations. We use indexes to access the elements of the array. Positive indexing starts from (0 to size - 1), and negative indexing starts from the end of the array from (-1 to -size). The name of an Array is treated as a pointer by the C compiler, pointing to the first element of the data structure. Hence, we can use pointer arithmetic to access Array elements. Now, let us dive into some coding questions: 1) Write a function to identify the first element in the array that is not smaller than its neighbors.Solution: At least one such element will exist in an array, called the "Peak element". We can use many approaches, like iterating the whole array, finding the largest element in the array, etc., to find the element. Here, we'll use the best approach. Remember that we are just asked to write a function. Hence, we can take the array and its size as the two parameters and write the function: Way to think: We need an efficient code. It takes O(n) time if we iterate the whole array. But we don't need all the peak elements; we need one. We have to keep searching for the element eliminating parts of the array. We used the Binary search algorithm to eliminate the parts of the array we don't have to traverse. Even though the array isn't sorted, we'll have to find one locally large element. Code: 2) Write a function to reverse the elements of an array using recursion.Solution: We can declare two variables, l = 0, h = (n - 1). We can keep swapping the values in the array at l and h by incrementing l and decrementing h till both l and h reach the middle element or till both overtake each other. Function: 3) Write a function to segregate an array of 0's and 1's by traversing the array only once.Solution: We're asked to traverse the array only once. We need to use two pointers/ variables to traverse the array. We have two options here:
Variables from both ends of the array: Way to think: We use two variables l and h storing the indexes of the array's first and last elements, respectively. We want to set 0's followed by 1's in the array. The elements traversed by l from the beginning should be 0's, and the elements from the end visited by h should be 1s. We need to swap the elements if l finds a 1 or h finds a 0. Both variables from the beginning of the array: Way to think: We want to arrange 0's followed by 1's in the array. We'll use two variables, both traversing from the beginning of the array. One variable is used to visit the elements and check the values, and the other is dedicated to staying at an index. The logic here is that whenever the visiting variable finds a 0, we should swap the elements at two variables. Observe that in both methods, we traversed the whole array only once. 4) Write a C function to move all the negative elements to one side of the given integer array.Solution: We can solve this problem like the above problem. We're given an integer array with positive and negative integers arranged randomly. We're supposed to arrange the array in the order of positive integers followed by negative or negative integers followed by positive integers. In the above problem, we wrote code to arrange an array with 0's followed by 1's. Here, we need to use the same algorithm to arrange an array with negative elements followed by positive elements. Variables from both ends of the array: Both variables from the beginning of the array: 5) Write a C function to find the maximum sum of k consecutive elements in the given array.Solution: We're given an integer array and an integer k. We need to find the maximum sum possible in the array when elements in k consecutive indexes are added. Using nested loops, we can use the brute-force approach to check all the k-sized possible sub-arrays in the array. It could be more efficient. Another notable technique we can use is the "Sliding window technique". Sliding Window technique: We take the value of k from the user, and the concept here is that we create a window of size k, and we'll keep sliding it by a unit index. For example: Suppose we need the maximum sum of 2 consecutive indexes, create a 2-sized window, and keep sliding (traversing) it throughout the array. We'll find the sum of elements of each window and return the maximum sum: Code: 6) Write a C function to find a Sub-array with a given sum from an array of non-negative numbers.Solution: We're given an array of only positive numbers and the required sum. We need to find a sub-array with elements adding to the given sum. There can be more than one such sub-array. We can use nested loops and check the sum of all possible sub-arrays, but it could be more efficient. Hence, we can use the above "Sliding window technique." Way to think: It is given that all the elements in the array are positive. So, if there is a sub-array with a sum greater than the given sum, that's it. We need to leave the elements as they are. Hence, we can use this condition if the array also has negative elements; we cannot use the sliding window technique in all cases as the sum keeps changing.
Traversal: For example: Code: 7) Write a C function to find the occurrence of a given integer in the given sorted array.Solution: We're given a sorted array and an integer. Our task is to find the number of occurrences of the integer in the array. It is a simple problem. We can solve it using any search algorithm we want. Way to think: We can solve the problem using the linear search or binary search. It is an optimization of traditional binary sort for better efficiency. But, we need to use an improved binary search algorithm to solve the problem in O(Log n) time. Algorithm:
Code: Three functions:
8) Write an efficient function in C to find the LCM using the GCD of two numbers.Solution: The whole logic lies in the relation between LCM and GCD of two numbers. Suppose a and b are the two numbers: a * b = LCM (a, b) * GCD (a, b) LCM (a, b) = a * b / GCD (a, b) 9) Write a C function to find the sum of digits of a number and print the number reversed in C.Solution: To find the sum of all the number's digits and reverse them, we need to separate them from the number. We can access the digits of a number using division and modulus with multiples of 10: Code: 10) Write a C function to determine if a given year is a leap year.Solution: Logic to check if a given year is a leap year or not:
Code: |