Perficient interview question

Find the unique value in an array

Interview Answers

Anonymous

1 Jul 2015

Example: while (start Check for equal to target value / \ (1) (3) | | | ----> start = (this leaf index) : if target is > parent | ----> end = (this leaf index) : if target =. Use index 8 and 3 in the array above as two prime examples. Furthermore, it would take a stroke of luck to find the unique value "1" take this for example: You have a struct named MyStruct with members ID which is type int and Value which is type int. If you randomly generated 'n' number of these and all except one were duplicates, how would you find the unique value?

Anonymous

1 Jul 2015

public struct MyStruct : IEquatable, IComparable { // Saved for the sake of not having to recalculate every time its called (Specifically for this example.) private int _hashCode; public MyStruct(int id, int value) { ID = id; Value = value; _hashCode = 0; } public int ID { get; } public int Value { get; } public int CompareTo(MyStruct other) { return GetHashCode() == other.GetHashCode() ? 0 : GetHashCode() (MyStruct a, MyStruct b) { return a.GetHashCode() > b.GetHashCode(); } public static bool operator {Value}"; } public override bool Equals(object obj) { return obj is MyStruct && obj.GetHashCode() == GetHashCode(); } #endregion }

Anonymous

1 Jul 2015

internal class Program { private static void Main(string[] args) { // Create a unique value var unique = new MyStruct("Unique".GetHashCode(), 0x200); // Prepare for all test cases var executionArray = new Func[] { executeFindCustomStructWithLinq, executeFindUniqueWithHashSet, executeFindUniqueWithBinaryTree }; // Execute test cases and get results var resultsTypeArray = executionArray.Select( method => new {Name = nameof(method), Result = method(unique)}); // print the results from the case resultsTypeArray.ToList().ForEach(item => WriteResults(item.Name,unique,item.Result)); Console.ReadKey(); } private static void WriteResults(string methodName, MyStruct orig, MyStruct target) { Console.WriteLine($"{methodName}: {orig} == {target}"); } private static IList CreateDataSet(MyStruct unique) { var random = new Random(); var amount = random.Next(0X0, 0x100); var myStructFactory = MyStructFactory.Instance; var dataSet = new List((amount*2) + 1); dataSet.AddRange(myStructFactory.Create(amount)); // create a copy of the base data set var holdingBuffer = new MyStruct[dataSet.Count]; dataSet.CopyTo(holdingBuffer, 0); // Insert Unique somewhere in the set dataSet.Insert(random.Next(0, dataSet.Count), unique); // add duplicated base dataset dataSet.AddRange(holdingBuffer); return dataSet; } #region ExecutionExamples private static MyStruct executeFindUniqueWithBinaryTree(MyStruct unique) { // Keep in mind this would only work if we knew the value before trying to parse the data set. var dataSet = CreateDataSet(unique); var orderedDataSet = dataSet.OrderBy(@struct => @struct).ToArray(); var start = 0; var identifiedUnique = new MyStruct(); var end = orderedDataSet.Count(); while (start (); var identifiedUnique = new MyStruct(); for (var i = 0; i a ^= b); return identifiedUnique; } #endregion }

Anonymous

1 Jul 2015

internal class MyStructFactory { internal const int CharMin = 0x20; internal const int CharMax = 0x7f; private readonly Random _random = new Random(); static MyStructFactory() { Instance = new MyStructFactory(); } private MyStructFactory() { } public static MyStructFactory Instance { get; } public MyStruct Create() { return new MyStruct(CreateName().GetHashCode(), CreateValue()); } public IEnumerable Create(int amount) { return Enumerable.Range(0, amount).Select(i => Create()); } private string CreateName() { var name = new char[_random.Next(0x4, 0xa)]; for (var i = 0; i < name.Length; i++) { name[i] = (char) _random.Next(CharMin, CharMax); } return new string(name); } private int CreateValue() { return _random.Next(0x80, 0x100); } }

Anonymous

1 Jul 2015

Edit: 3. HashSet implemented solution: Your code fails to successfully complete the task. Your output is similar to "0,1,2,3,4,5". As you can see, your implementation did not pull the unique values out of the array, but only executed as (remove { Distinct }) (add { HashSet }) was designed to, it removed the duplicate values.