Data Structures & AlgorithmsVery hardProduct placement · Amazon/Microsoft
What is the minimum number of swaps (of any two elements) needed to bring all the 1s of [1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0] together into one contiguous block? (Slide a window whose size equals the number of 1s.)
- A. 3
- B. 4
- C. 2
- D. 1
Correct answer: C. 2
There are 6 ones, so the final block has length 6. The window of that length containing the most 1s needs one swap per 0 inside it: 2.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Reverse the order of words, collapsing multiple spaces. What is printed? python s = " new the is oil " w = s.split() print("|" + "…
- You must answer many queries 'sum of A[l..r]' while also updating single elements, on an array of n elements. Which choice gives O(log n)…
- An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 7, 4, 3, 2, 1, 1]. Treating i → a[i] as a linked…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "aabaabbbabab", "bab" over = sum(1 for i in…
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…
- A fixed window of length 3 slides over s = "baacbbbcbcabab" with a character-count array. At how many starting positions is the window an…