I had an interesting interview question the other day, which I really struggled with. The (very ambitious) specification required me to write in C # parsers for two different data streams. Here is an example of the first stream:
30=EUR/USD,35=3,50=ON,51=12.5,52=13.5,50=6M,51=15.4,52=16.2,50=1Y,51=17.2,52=18.3
where 30 is the currency pair, 35 is the number of tenors, and 50.51.52 is the tenor, bid and request, respectively. The bid and request are optional, but the correct tenor-bid-ask tuple will have at least one of two prices. The infrastructure code they presented meant that the result of parsing this line should be 3 separate objects (instances of DataElement). I ended up with a rather nasty switch-statement statement and a loop-based implementation that I'm not sure it really worked.
What methods are there for reading this kind of thread? I tried to figure out something with recursion that I could not understand.
EDIT: Based on @evanmcdonnall's answer (accepted), here is the full compilation and operation code if it is useful to someone else.
List<DataElement> Parse(string row)
{
string currency=string.Empty;
DataElement[] elements = null;
int j = 0;
bool start = false;
string[] tokens = row.Split(',');
for (int i = 0; i < tokens.Length; i++)
{
string[] kv = tokens[i].Split('=');
switch (kv[0])
{
case "30":
currency = kv[1];
break;
case "35":
elements = new DataElement[int.Parse(kv[1])];
break;
case "50":
if (start)
j++;
elements[j] = new DataElement() { currency = currency, tenor = kv[1] };
start = true;
break;
case "51":
elements[j].bid = double.Parse(kv[1]);
break;
case "52":
elements[j].ask = double.Parse(kv[1]);
break;
}
}
return elements.ToList();
}
Key concepts:
- Have a separate counter for the "inner loop" of repeating elements in each row
- Have a boolean flag indicating when this "inner loop" begins.
- Select an array of objects to store the results of the "inner loop" at the point where the length is known (ie tag 50)
- For simplicity and clarity, create a function that reads only one line, and then call it several times from a separate function.
source
share