1. check whether a string is palindrome 2. reverse words in a string
Anonymous
This implementation does not use StringBuilder or other built-in String operations if you tell your interviewer you are going to use reverse on a StringBuider, you are toast. using Microsoft.VisualStudio.TestTools.UnitTesting; namespace AlgorithmsTests { [TestClass] public class ReverseAndPalindrome { [TestMethod] public void Palindrome_Tests() { Assert.IsTrue(IsPalindrome(string.Empty)); Assert.IsTrue(IsPalindrome("a")); Assert.IsTrue(IsPalindrome("aa")); Assert.IsTrue(IsPalindrome("aba")); Assert.IsTrue(IsPalindrome("abcdefghgfedcba")); Assert.IsFalse(IsPalindrome("ab")); Assert.IsFalse(IsPalindrome("abbcba")); } [TestMethod] public void ReverseSteing_Tests() { Assert.AreEqual("a", ReverseString("a")); Assert.AreEqual("ab", ReverseString("ba")); Assert.AreEqual("fedcba", ReverseString("abcdef")); } private string ReverseString(string o) { //switch the first half against the second half char[] s = o.ToCharArray(); for (int i = 0; i < s.Length / 2; i++) { char t = s[i]; s[i] = s[s.Length - 1 - i]; s[s.Length - 1 - i] = t; } return new string(s); } private bool IsPalindrome(string s) { //check the fisrt half against the second half for (int i = 0; i < s.Length / 2; i++) { if (s[i] != s[s.Length - 1 - i]) return false; } return true; } } }
Check out your Company Bowl for anonymous work chats.