Amazon Interview Question

Replace all spaces in a string by %20. Write production quality code in first attempt

Interview Answers

Anonymous

Jun 11, 2012

check out the code.. http://justprogrammng.blogspot.in/2012/06/replace-all-spaces-in-string-by-20.html

2

Anonymous

May 19, 2012

count the number of spaces in the string, say this number is n allocate a new string whose length is the same + 2 * n maintain two indices, i and j, i starts at the start of the original string and j starts at the start of the new string while i is less than the length of the string: if symbol at i is a space: copy %20 to the new string at j, advancing j by 3 else copy symbol at i in string to symbol at j in new string, advancing j by 1 advance i by 1

1