Microsoft Interview Question
Software Engineer in TestsFor the reversal question, it can be done in-place with a running time of O(nm) where n is the average number of characters in a word and m is the number of words in the sentence.
Loop through the characters, if the char is a delimiter, reverse the word till that point. Repeat the reversal at the end of the word. (Since there will be no delimiter at the end of the sentence)
For the random problem, convert the two dates to TotalMilliseconds.
Then do
(GetRandom() % (EndDate - StartDate)) + StartDate
This will give a random date between StartDate and EndDate.
Here, we assume that GetRandom() returns a random, unsigned long.
DateTime d1 = new DateTime(2004, 02, 23);
DateTime d2 = new DateTime(2008, 06, 20);
Random r = new Random();
System.TimeSpan diff = (d2 - d1);
double milli = diff.TotalMilliseconds;
double rand = r.NextDouble() * milli;
DateTime d3 = new DateTime(d1.Year,d1.Month,d1.Day);
d3 = d1 + TimeSpan.FromMilliseconds(rand);
Console.WriteLine(d3);
do not assume that "space" is the only delimiter????
- Ratn October 11, 2008Means you dont have any extra space or what ?