Jayjay
Contributor
- Joined
- Apr 7, 2002
- Messages
- 7,173
- Location
- Finland
- Basic Beliefs
- An accurate worldview or philosophy
I recently had a programming test as part of a job interview. I failed this particular puzzle. This bothered me, so I tried to google for an answer afterwards, but no luck, so maybe I could throw it to you geniuses.
Given number k, and a string of 1s and 0s of at least size k, find a substring of at least size k with the highest proportion of 1s. If there are multiple possible answers, any one of them will do.
It's trivial to just test all the substrings, but the algorithm needs to run faster than that. During the actual test, I failed this criterion for some inputs (I was not given the actual test cases). Since the test is no longer online, I can't refer to it or run other experiments.
So for example, if k=4, and the given string is "0110011" the answer is substring "110011".
How would you go about solving the problem? I'm not looking for a rigorous mathematical proof, just an outline or an idea would do.
Given number k, and a string of 1s and 0s of at least size k, find a substring of at least size k with the highest proportion of 1s. If there are multiple possible answers, any one of them will do.
It's trivial to just test all the substrings, but the algorithm needs to run faster than that. During the actual test, I failed this criterion for some inputs (I was not given the actual test cases). Since the test is no longer online, I can't refer to it or run other experiments.
So for example, if k=4, and the given string is "0110011" the answer is substring "110011".
How would you go about solving the problem? I'm not looking for a rigorous mathematical proof, just an outline or an idea would do.