Interview Question
Country: United States
{
ListVals LstItmLstItm = new ListVals();
List<ListVals> TheSource = new List<ListVals>();
AddToNode("2", "a", TheSource);
AddToNode("4", "c", TheSource);
AddToNode("6", "e", TheSource);
AddToNode("2", "b", TheSource);
AddToNode("4", "d", TheSource);
AddToNode("6", "f", TheSource);
var JustNodes = (from ListVals in TheSource
select new
{
NodeOnly = ListVals.NodeId.ToString()
}
).Distinct().AsEnumerable().ToList();
var MatchedNodes = (from NdGrp in JustNodes
join LstItm in TheSource on NdGrp.NodeOnly equals LstItm.NodeId
select new
{
NodeKey = NdGrp.NodeOnly.ToString(),
NodeVals = LstItm.NodeVal.ToString()
});
foreach (var NodeEntry in MatchedNodes)
{
Console.WriteLine(" Node = " + NodeEntry.NodeKey.ToString() + ", Value = " + NodeEntry.NodeVals.ToString());
}
string Inkey = Console.ReadKey().ToString();
}
static void AddToNode(string NodeKey, string NodeVals, List<ListVals>TheStack)
{
ListVals ListItem = new ListVals();
ListItem.NodeId = NodeKey;
ListItem.NodeVal = NodeVals;
TheStack.Add(ListItem);
}
}
public class ListVals
{
public string NodeId { get; set; }
public string NodeVal { get; set; }
}
}
What company? Phone or onsite?
- Anonymous November 21, 2013