Microsoft Interview Question: Reverse all the characters in... | Glassdoor

Interview Question

Software Development Engineer Intern Interview Seattle, WA

Reverse all the characters in a string before you encounter

  an 'x'. abcdxdd - > dcbaxdd
Answer

Interview Answer

2 Answers

0

using System;
using System.Linq;

namespace ReverseString
{
    class Program
    {
        static void Main()
        {
            string s = "abcdxdd";

            int reverseEnd = s.IndexOf('x');
            if (reverseEnd < 0)
                reverseEnd = 0;

            string subAr1 = new string(s.Substring(0, reverseEnd).Reverse().ToArray());
            string subAr2 = s.Substring(reverseEnd);
            s = String.Concat(subAr1, subAr2);

            Console.WriteLine(s);
        }
    }
}

Andrey on Feb 13, 2012
0

use doubly linked-list.

Start reading string each char by char. Add each cahr in node. once u find its 'X', At that time your current linked-list pointer will be on "d" i.e before 'X'. Start reading the list in reverse order.

Vidhi on Sep 24, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.