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, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1] together into one contiguous block? (Slide a window whose size equals the number of 1s.)
- A. 4
- B. 1
- C. 3
- D. 2
Correct answer: D. 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
- Can a string become a palindrome by deleting at most one character? Two pointers try skipping either side on the first mismatch. What is…
- Items K, L, M, N, O are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- In a singly linked list with only a head pointer, which operation CANNOT be done in O(1) time?
- Run-length encoding: each maximal run is written as the character followed by its length. What is printed? python s = "yyyyyyyyxx" out, i…
- In the binary array [0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1] you may flip at most 2 zeros to ones. What is the length of the longest run…