2009年6月2日 星期二

Software Company Interview Question




using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ApplicantTestin
{
/// The DataObject class stored with a key
class DataObject
{
// Populate
private string keyStr;
private long keyValue;
private bool keyStatus;



public DataObject(string inputKey)
{
keyStr = inputKey;
}

public string Str
{
get { return keyStr; }
}
public long Val
{
get { return keyValue; }
set { keyValue = value; }
}
public bool Status
{
get { return keyStatus; }
set { keyStatus = value; }
}


public void Decrease()
{
keyValue--;
}
public void Increase()
{
keyValue++;
}


}


class Program
{
static Hashtable Data = new Hashtable();

static string[] StaticData = new string[] { "X-ray","Echo","Alpha", "Yankee","Bravo", "Charlie",
"Delta", "Hotel", "India", "Juliet", "Foxtrot","Sierra",
"Mike","Kilo", "Lima", "November", "Oscar", "Papa", "Qubec",
"Romeo", "Tango","Golf", "Uniform", "Victor", "Whisky",
"Zulu"};

static void Main(string[] args)
{
for (int i = 0; i < StaticData.Length; i++)
Data.Add(StaticData[i].ToLower(), new DataObject(StaticData[i]));
while (true)
{
PrintSortedData();
Console.WriteLine();
Console.Write("> ");
string str = Console.ReadLine();
string[] strs = str.Split(' ');

if (strs[0] == "q")
break;
else if (strs[0] == "printv")
PrintSortedDataByValue();
else if (strs[0] == "print")
PrintSortedData();
else if (strs[0] == "inc")
Increase(strs[1]);
else if (strs[0] == "dec")
Decrease(strs[1]);
else if (strs[0] == "swap")
Swap(strs[1], strs[2]);
else if (strs[0] == "ref")
Ref(strs[1], strs[2]);
else if (strs[0] == "unref")
UnRef(strs[1]);
}
}

/// <summary>
/// Create a reference from one data object to another.
/// </summary>
/// <param name="key1">The object to create the reference on</param>
/// <param name="key2">The reference object</param>
static void Ref(string key1, string key2)
{
try
{
if (Data.ContainsKey(key1) && Data.ContainsKey(key2))
{
Data[key1.ToLower()] = Data[key2.ToLower()];
Console.WriteLine(key1 + " -> <" + ((DataObject)Data[key1.ToLower()]).Str + ">");
Console.WriteLine(key2 + " -> <" + ((DataObject)Data[key2.ToLower()]).Str + ">");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

/// <summary>
/// Removes an object reference on the object specified.
/// </summary>
/// <param name="key">The object to remove the reference from</param>
static void UnRef(string key)
{
try
{
if (Data.ContainsKey(key))
{
string tmpstr = key.Substring(0, 1).ToUpper() + key.Substring(1, key.Length - 1);
Data[key.ToLower()] = new DataObject(tmpstr);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}

}

/// <summary>
/// Swap the data objects stored in the keys specified
/// </summary>
static void Swap(string key1, string key2)
{
// Populate
try
{
if (Data.ContainsKey(key1) && Data.ContainsKey(key2))
{
DataObject obj = (DataObject)Data[key1.ToLower()];
Data[key1.ToLower()] = Data[key2.ToLower()];
Data[key2.ToLower()] = obj;
Console.WriteLine(key1+ " <-> " + key2);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

/// <summary>
/// Decrease the Value field by 1 of the
/// data object stored with the key specified
/// </summary>
static void Decrease(string key)
{
try
{
if (Data.ContainsKey(key))
{

((DataObject)Data[key.ToLower()]).Decrease();
Console.WriteLine("Decrement: " + ((DataObject)Data[key.ToLower()]).Val);

}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

/// <summary>
/// Increase the Value field by 1 of the
/// data object stored with the key specified
/// </summary>
static void Increase(string key)
{
try
{
if (Data.ContainsKey(key))
{
((DataObject)Data[key.ToLower()]).Increase();
Console.WriteLine("Increment: "+ ((DataObject)Data[key.ToLower()]).Val);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}

}


/// <summary>
/// Prints the information in the Data hashtable to the console.
/// Output should be sorted by key
/// References should be printed between '<' and '>'
/// The output should look like the following :
///
///
/// Alpha...... -3
/// Bravo...... 2
/// Charlie.... <Zulu>
/// Delta...... 1
/// Echo....... <Alpha>
/// --etc---
///
/// </summary>
static void PrintSortedData()
{
ArrayList sortList = new ArrayList();
sortList.AddRange(Data.Keys);
sortList.Sort();
foreach (string s in sortList)
{
DataObject obj = (DataObject)Data[s];
string tmpstr = s.Substring(0,1).ToUpper()+s.Substring(1,s.Length-1);

if(obj.Str == tmpstr){
Console.WriteLine(tmpstr + " " + obj.Val);
}else{
Console.WriteLine(tmpstr + " <" + obj.Str+">");
}
}
}


/// <summary>
/// Prints the information in the Data hashtable to the console.
/// Output should be sorted by stored value
/// References should be printed between '<' and '>'
/// Sorting order start from max to min, larger value takes priority.
/// The output should look like the following :
///
///
/// Bravo...... 100
/// Echo...... 99
/// Zulu...... 98
/// Charlie.... <Zulu>
/// Delta...... 34
/// Echo....... 33
/// Alpha...... <Echo>
/// --etc---
///
/// </summary>
static void PrintSortedDataByValue()
{
ArrayList sortlistValue = new ArrayList();
sortlistValue.AddRange(Data.Keys);
for (int i = sortlistValue.Count-1; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
if (((DataObject)Data[sortlistValue[j+1]]).Val > ((DataObject)Data[sortlistValue[j]]).Val){
string tmp = sortlistValue[j].ToString();
sortlistValue[j] = sortlistValue[j+1];
sortlistValue[j+1] = tmp;
}
}

}

Console.WriteLine("Sorted by value\n====================");

foreach (string s in sortlistValue)
{
DataObject obj = (DataObject)Data[s];
string tmpstr = s.Substring(0, 1).ToUpper() + s.Substring(1, s.Length - 1);

if (obj.Str == tmpstr)
{
Console.WriteLine(tmpstr + " " + obj.Val);
}
else
{
Console.WriteLine(tmpstr + " <" + obj.Str + ">");
}
}
Console.WriteLine("====================");
}
}
}

沒有留言:

張貼留言