Array Questions

These questions are generally asked in university exam

Indexed based Array question from topic DSA IN C

11. Find the address of X[20] if the base address of a one-dimensional array X is 5000, each element occupies 4 bytes, and the array indexing starts at -5.

Answer:

To find the address, we use:

Address of X[i] = Base Address + ((i - Starting Index) * Size of each element)

Here, base address = 5000, starting index = -5, i = 20, and size of each element = 4 bytes.

Address of X[20] = 5000 + ((20 - (-5)) * 4) = 5000 + (25 * 4) = 5000 + 100 = 5100

So, the address of X[20] is 5100.

12. Find the address of A[30] if the base address of array A is 1000, each element occupies 2 bytes, and the array starts at index -10.

Answer:

To find the address, we use:

Address of A[i] = Base Address + ((i - Starting Index) * Size of each element)

Here, base address = 1000, starting index = -10, i = 30, and size of each element = 2 bytes.

Address of A[30] = 1000 + ((30 - (-10)) * 2) = 1000 + (40 * 2) = 1000 + 80 = 1080

So, the address of A[30] is 1080.

13. Find the address of B[10] if the base address of array B is 4000, each element occupies 6 bytes, and the array starts at index -20.

Answer:

To find the address, we use:

Address of B[i] = Base Address + ((i - Starting Index) * Size of each element)

Here, base address = 4000, starting index = -20, i = 10, and size of each element = 6 bytes.

Address of B[10] = 4000 + ((10 - (-20)) * 6) = 4000 + (30 * 6) = 4000 + 180 = 4180

So, the address of B[10] is 4180.

14. Find the address of C[15] if the base address of array C is 2000, each element occupies 5 bytes, and the array starts at index -3.

Answer:

To find the address, we use:

Address of C[i] = Base Address + ((i - Starting Index) * Size of each element)

Here, base address = 2000, starting index = -3, i = 15, and size of each element = 5 bytes.

Address of C[15] = 2000 + ((15 - (-3)) * 5) = 2000 + (18 * 5) = 2000 + 90 = 2090

So, the address of C[15] is 2090.

15. Find the address of D[40] if the base address of array D is 1500, each element occupies 3 bytes, and the array starts at index -15.

Answer:

To find the address, we use:

Address of D[i] = Base Address + ((i - Starting Index) * Size of each element)

Here, base address = 1500, starting index = -15, i = 40, and size of each element = 3 bytes.

Address of D[40] = 1500 + ((40 - (-15)) * 3) = 1500 + (55 * 3) = 1500 + 165 = 1665

So, the address of D[40] is 1665.

16. What is the time complexity to insert an element at the end of an array?

Answer:

Inserting an element at the end of an array takes constant time, as no shifting of elements is needed. The time complexity for this operation is O(1).

17. How do you delete an element from the middle of an array and what is the time complexity?

Answer:

To delete an element from the middle of an array, all elements after the deleted element must be shifted one position to the left. The time complexity for this operation is O(n), where n is the number of elements in the array.

18. What is the maximum number of comparisons required to search for an element in an unsorted array of size n?

Answer:

In an unsorted array, the element being searched for could be at any position or may not be present. In the worst case, we would have to compare the target element with all elements of the array. Therefore, the maximum number of comparisons is n.

19. How can you reverse an array in place?

Answer:

To reverse an array in place, you can swap the elements at the start and the end of the array, then move inward until all elements have been swapped. The time complexity of this operation is O(n), where n is the number of elements in the array.

20. What is the time complexity for accessing an element in an array using its index?

Answer:

Accessing an element in an array using its index takes constant time, as we can directly compute the memory address using the formula. The time complexity is O(1).

21. How does the insertion of an element at the beginning of an array affect time complexity?

Answer:

Inserting an element at the beginning of an array requires shifting all other elements one position to the right. This makes the time complexity of the insertion operation O(n), where n is the number of elements in the array.