ProgrammingHardAccenture · Cognizant
A binary string s = "1100101" is to be made alternating. Two targets are possible: "0101010..." and "1010101...", each the same length as s. Flipping one bit costs 1. The minimum number of flips is printed.
A binary string s = "1100101" is to be made alternating. Two targets are possible: "0101010..." and "1010101...", each the same length as s. Flipping one bit costs 1. The minimum number of flips is printed.
flips0 = mismatches with pattern starting 0 flips1 = mismatches with pattern starting 1 Print min(flips0, flips1)
- A. 5
- B. 4
- C. 3
- D. 2
Correct answer: D. 2
Against 1010101 the string has only 2 mismatches, and against 0101010 it has 5, so the minimum is 2. Using only the 0-start target gives 5, counting the ones gives 4, and counting the zeros gives 3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Count how many copies of the word BALLOON can be formed from the letters of "lloolldnaubbalo" (each letter used at most once per copy)…
- Children have ratings r = [1, 0, 2, 2, 1]. Each child gets at least one candy, and a child with a higher rating than a neighbour gets more…
- Let x = 0. For i from 1 to 10: if i mod 3 == 0 then x = x + i, otherwise x = x - 1. What is printed? x = 0 for i = 1 to 10 if i mod 3 == 0…
- A rope of length L is cut into two parts of lengths floor(L/2) and L - floor(L/2) whenever its length is 2 or more; the cost of a cut is…
- Jobs each take one unit of time and carry a deadline: 1, 1, 2, 3, 4, 4 (the last slot the job may use; slots are 1, 2, 3, ...). The…
- Events (start, end) are attended one per day, and an event may be attended on any day from start to end inclusive. Events: (1,2), (1,2)…