ARRAY || BASIC PROBLEMS Flashcards
If you’re given an array like [10, 20, 30, 40, 50], how would you conceptually explain what “printing alternate elements” even means?
You start by printing the first element (index 0), then skip the next element (index 1), print the third element (index 2), skip the next, and continue like that until the end of the array.
What pattern in the indices do you notice when you’re printing alternate elements? How does that help you plan the for loop?
The indices are 0, 2, 4, 6, etc., so the loop needs to start at index 0 and increment by 2 each time (i += 2) to print every alternate element.
How would you write a for loop in JavaScript that prints every alternate element from an array starting at index 0? (✍🏽 Write the loop structure!)
for (let i = 0; i < arr.length; i += 2) {
console.log(arr[i]);
}
Why do we do i += 2 instead of i++ in the loop when finding alternate elements?
We do i += 2 to skip every second element, so we’re only accessing elements at even indices (0, 2, 4, etc.), which gives us the alternate elements.
What is the time complexity and the space complexity of the iterative solution for printing alternate elements of an array? And why?
Time Complexity: O(n), because we are iterating through the array once.
Space Complexity: O(1), because we’re not using any extra space for the iteration itself (just a few variables).
If you’re using recursion instead of iteration for printing alternate elements of an array, how does your “jump to the next alternate element” happen inside the recursive function?
In recursion, you print the current element, then make a recursive call with index + 2 to jump to the next alternate element.
In the recursive version for printing alternate elements of an array, what two key things happen in each call before making the next call?
You push the current element (based on index) into the result.
You call the recursive function with index + 2.
Why does the recursive solution have O(n) space complexity even though the iterative one has O(1) space complexity for printing alternate elements of an array?
The recursive solution uses additional space for each recursive call in the call stack, which grows with the size of the array, resulting in O(n) space complexity. The iterative solution only uses a few variables, so it’s O(1).
Write the skeleton of the recursive function that handles alternate element printing in JavaScript. (Don’t write the whole code, just the basic function signature and idea.)
function printAlternatesRecursive(arr, index) {
if (index < arr.length) {
console.log(arr[index]);
printAlternatesRecursive(arr, index + 2);
}
}
Given this array: [-5, 1, 4, 2, 12], what should the output be when you print the alternate elements starting from index 0?
The output should be: -5 4 12
If you had to explain the difference between the iterative and recursive approach for this for alternate elements to a friend who knows zero coding… what would you say in one sentence?
The iterative approach uses a for loop and updates the index manually, while the recursive approach calls the same function repeatedly with a new index, effectively jumping to the next alternate element.
You are asked in an interview:
“Could you optimize your recursive solution for alternate elements to not use O(n) space?”
What would you say? 👀
Yes, you could convert the recursion into an iterative loop, which would use O(1) space instead of O(n) by eliminating the need for a call stack.
What does it mean to “print alternate elements” of an array? Can you explain it like I’m a 5-year-old?
“Printing alternate elements” just means skipping every other element in the array. So, if you have an array like [10, 20, 30, 40, 50], you start by printing the first element (10), then skip the second element (20), then print the third element (30), skip the next (40), and print the fifth element (50). Basically, you’re just picking every odd-numbered position (0, 2, 4, etc.)
What is the Linear Search Algorithm used for in an array? Explain it step by step.
Linear Search is used to find the first occurrence of an element in an array. Here’s how it works:
Start from the first element of the array.
Compare each element with the target element (x).
If you find the element, return its index.
If you don’t find the element after checking all elements, return -1 to indicate it’s not present.
For example, in the array [10, 2, 3, 4] and looking for 3:
Check 10 → No.
Check 2 → No.
Check 3 → Yes! Return index 2.
Given the following JavaScript code, what will be the output for the array arr = [10, 8, 30] and x = 6?
function search(arr, n, x) {
for (let i = 0; i < n; i++)
if (arr[i] == x)
return i;
return -1;
}
let arr = [10, 8, 30];
let x = 6;
let n = arr.length;
let result = search(arr, n, x);
console.log(result);
The function will return -1 because 6 is not present in the array [10, 8, 30]. The output will be:
-1
What is the time complexity of Linear Search in the best, worst, and average cases?
Best Case: O(1)
The element is at the first index, so it’s found immediately.
Worst Case: O(n)
The element is at the last index or not in the array at all, so all elements must be checked.
Average Case: O(n)
On average, half of the array will be searched before finding the element (or determining it’s not there).
What are the time and space complexities of Linear Search?
Time Complexity:
Best Case: O(1)
Worst Case: O(n)
Average Case: O(n)
Space Complexity: O(1)
Linear Search doesn’t require extra memory, just a variable for iteration, so its space complexity is constant.
When should you prefer Linear Search over more optimized search algorithms (like Binary Search)?
Linear Search is preferred when:
The array is unsorted, and you need to find an element.
The dataset is small, so performance isn’t an issue.
You’re searching in linked lists, where elements aren’t in contiguous memory.
If the element x is not found in the array during a Linear Search, what does the algorithm return, and why?
If x is not found in the array, Linear Search returns -1. This indicates that the element does not exist in the array.
What are the advantages and disadvantages of using Linear Search?
Advantages:
Can be used on unsorted arrays.
No need for extra memory.
Simple to implement.
Disadvantages:
Slow for large arrays (O(n) time complexity).
Not efficient for large datasets compared to algorithms like Binary Search.
Given this code to search for x in an array arr[], what is the issue in the following function?
function search(arr, n, x) {
for (let i = 0; i <= n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
The issue is with the loop condition. It should be i < n, not i <= n.
Correct code:
for (let i = 0; i < n; i++)
This is because i starts from 0 and should not exceed the last index (n-1), or else it will lead to an out-of-bounds error.
How would you implement a recursive version of the Linear Search Algorithm?
Here’s a basic recursive implementation of Linear Search:
function searchRecursive(arr, index, x) {
if (index >= arr.length) return -1; // Base case: if index exceeds array length, return -1
if (arr[index] === x) return index; // If the element is found, return its index
return searchRecursive(arr, index + 1, x); // Recursive call for the next index
}
This function starts from index 0 and searches each element recursively until it finds the element or reaches the end of the array.
Why is Linear Search often used with unsorted lists?
Linear Search is used with unsorted lists because it doesn’t require the list to be sorted. It checks every element one by one, making it the most straightforward way to search in an unsorted collection.
What is the time complexity and space complexity of the iterative approach to finding the largest element in an array?
Time Complexity: O(n)
You traverse the array once, where n is the number of elements in the array.
Space Complexity: O(1)
The space used does not depend on the size of the array because you’re only storing the max value and an index during the iteration.