When interviewing for a programming flash job, it’s always good to know the basics. One question that’s often asked at a majority of jobs is to manipulate strings. This morning, I quickly created a demo of some possible interview questions that may be asked of you, with the solution of how they were all done below. It includes:

  • Reverse Text – Make each character appear in reverse order, essentially “flip” the sentence
  • Reverse Words – Keep the text in the correct order, but show each full word from end to beginning
  • Strip Whitespaces – Remove all spaces in the text
  • Remove Duplicate Characters – If a character has already been used, remove all subsequent instances
  • 1st Non-Repeating Character – Show the first character that is only used ONCE in the entire string
  • Palendrome Check – Shows if the input contains a simple Palendrome (i.e. ‘bobcat’ should display ‘bob’

 

package com {

	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class Main extends MovieClip {

		private var currentCharacter:String;

		public function Main() {
			addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			manipulate_btn.addEventListener(MouseEvent.CLICK, manipulateText);
		}

		//choose manipulation type
		private function manipulateText(evt:MouseEvent):void
		{
			if(dataBox.selectedIndex == -1)
			{
				output.text = "Choose type from the dropdown";
			}
			else
			{
				switch(dataBox.value)
				{
					case "REVERSE_TEXT":
						output.text = reverseText(mainText.text);
						break;
					case "REVERSE_WORDS":
						output.text = reverseWords(mainText.text);
						break;
					case "STRIP_WHITESPACES":
						output.text = stripWhitespaces(mainText.text);
						break;
					case "REMOVE_DUPES":
						output.text = removeDuplicateCharacters(mainText.text);
						break;
					case "FIRST_NON_REPEATING_CHAR":
						output.text = firstNonRepeatingCharacter(mainText.text);
						break;
					case "PALENDROME_CHECK":
						output.text = palendromeCheck(mainText.text);
						break;						
					default:
						output.text = "Choose type from the dropdown below";
						break;

				}
			}

		}

		private function palendromeCheck(txt:String):String
		{
			for(var i:int = 1; i<txt.length-1; i++)
			{
				//odd length palendrome
				if(txt.charAt(i-1) == txt.charAt(i+1))
				{
					return "Contains Palendrome: '" + txt.substr(i-1, 3) + "'";
				}
			}
			//otherwise no palendrome
			return "NO PALENDROME";
		}

		private function firstNonRepeatingCharacter(txt:String):String
		{
			var obj:Object = {};

			//count into object
			for(var i:int = 0; i< txt.length; i++)
			{
				currentCharacter = txt.charAt(i).toLocaleLowerCase();

				if(!obj.hasOwnProperty(currentCharacter))
				{
					obj[currentCharacter] = 1;
				}
				else
				{
					obj[currentCharacter] ++;
				}
			}

			//scan through to see if character repeated
			for(var j:int = 0; j <txt.length; j++)
			{
				currentCharacter = txt.charAt(j).toLocaleLowerCase();
				{
					if(obj[currentCharacter] == 1)
					{
						return currentCharacter;
					}

				}

			}

			//if we get to this point, then every character repeats
			return "All characters repeat";

		}

		private function removeDuplicateCharacters(txt:String):String
		{
			var newString:String = "";
			var obj:Object = {};

			for (var i:int = 0; i < txt.length; i++)
			{
				currentCharacter = txt.substr(i,1);
				if(!obj.hasOwnProperty(currentCharacter))
				{
					obj[currentCharacter] = true;
					newString += currentCharacter;
				}

			}

			return newString;
		}

		private function reverseText(txt:String):String
		{
			var reverseString:String ="";
			for(var i:int = txt.length; i>-1; i--)
			{
				reverseString += txt.charAt(i);
			}
			return reverseString;
		}

		private function stripWhitespaces(txt:String):String
		{
			var a:Array = txt.split(" ");
			var string:String = "";

			for(var i:int = 0; i < a.length; i++)
			{
				string += a[i];
			}
			return string;
		}

		private function reverseWords(txt:String):String
		{
			var a:Array = txt.split(" ");
			var reverseString:String = "";

			while(a.length > 0)
			{
				reverseString += String(a[a.length-1]) + " ";
				a.pop();
			}

			return reverseString;
		}
	}

}