C# Programming Challenge – Print Object Dump

  • A+
Category:Languages

I recently went for an interview and was given the following challenge:

Method Signature:

public static string DumpList(string prefix, object list) 

Input:

prefix: "Foo" list: new object[] { "a string", new[] { "a", "b", "c" }, "spam", new[] { "eggs" }, new[] { new[] { "one", "two" }, new[] { "three", "four" } } } 

Expected Output:

Foo.0: a string Foo.1.0: a Foo.1.1: b Foo.1.2: c Foo.2: spam Foo.3.0: eggs Foo.4.0.0: one Foo.4.0.1: two Foo.4.1.0: three Foo.4.1.1: four 

Here is my attempt at answering this question:

public static string DumpList(string prefix, object list) {     int index = 0;     int index1 = 0;     int index2 = 0;      StringBuilder dumpListBuilder = new StringBuilder();      if (list is Array)     {         foreach (var obj in (Array)list)         {             if (obj is Array)             {                 foreach (var obj1 in (Array)obj)                 {                     if (obj1 is Array)                     {                         foreach (var obj2 in (Array)obj1)                         {                             dumpListBuilder.AppendFormat("{0}.{1}.{2}.{3}: {4}{5}", prefix, index, index1, index2, obj2, Environment.NewLine);                             index2++;                         }                     }                     else                     {                         dumpListBuilder.AppendFormat("{0}.{1}.{2}: {3}{4}", prefix, index, index1, obj1, Environment.NewLine);                     }                     index1++;                     index2 = 0;                 }             }             else             {                 dumpListBuilder.AppendFormat("{0}.{1}: {2}{3}", prefix, index, obj, Environment.NewLine);             }             index++;             index1 = 0;         }     }      return dumpListBuilder.ToString(); } 

It works, but isn't efficient and is limited to a depth of 3 nested lists. Can anyone suggest an optimal solution for infinite depth?

I am aware that I should be using recursion, but how do I do that without changing the method signature?


You could probably make it more readable, and more logical using recursion

Mine

public static string DumpList(string prefix, object list) {     if (list is object[] array)         return String.Join("/r/n", array.Select((value, i) => DumpList($"{prefix}.{i}", value)));     return $"{prefix}: {list}"; } 

MineOriginal

public static void DumpList2(string prefix, object input, List<string> result) {    if (input is object[] ary)       for (var i = 0; i < ary.Length; i++)          if (ary[i] is Array)             DumpList2($"{prefix}.{i}", ary[i], result);          else             result.Add($"{prefix}.{i}.{ary[i]}");    else       result.Add($"{prefix}.0.{input}"); } 

Benchmarks

Mode            : Release Test Framework  : .NET Framework 4.7.1 Benchmarks runs : 100 times (averaged)  Scale : 100 Name         |     Time |    Range | StdDev |    Cycles | Pass ----------------------------------------------------------------- MineOriginal | 0.550 ms | 0.311 ms |   0.15 | 1,853,187 | Yes Original     | 0.612 ms | 0.361 ms |   0.13 | 2,059,844 | Base Mine         | 0.697 ms | 0.012 ms |   0.08 | 2,368,041 | Yes SirRufo      | 0.776 ms | 0.022 ms |   0.15 | 2,635,368 | Yes Jimi         | 0.821 ms | 0.012 ms |   0.21 | 2,779,449 | Yes JimiNoFormat | 0.873 ms | 0.007 ms |   0.23 | 2,950,638 | Yes   Scale : 1,000 Name         |     Time |    Range | StdDev |     Cycles | Pass ------------------------------------------------------------------ MineOriginal | 4.461 ms | 0.541 ms |   0.21 | 15,185,195 | Yes Original     | 5.914 ms | 0.346 ms |   1.15 | 20,015,882 | Base Mine         | 6.725 ms | 0.153 ms |   0.31 | 22,890,735 | Yes JimiNoFormat | 6.818 ms | 0.335 ms |   0.23 | 23,193,642 | Yes Jimi         | 6.873 ms | 0.633 ms |   0.37 | 23,382,543 | Yes SirRufo      | 7.198 ms | 1.004 ms |   0.70 | 24,512,923 | Yes   Scale : 10,000 Name         |      Time |    Range | StdDev |      Cycles | Pass -------------------------------------------------------------------- Original     | 57.283 ms | 3.049 ms |   5.70 | 194,331,610 | Base MineOriginal | 67.833 ms | 5.618 ms |   2.66 | 230,951,862 | Yes Mine         | 72.695 ms | 2.624 ms |   3.39 | 246,760,230 | Yes Jimi         | 73.571 ms | 4.007 ms |   2.00 | 249,736,001 | Yes JimiNoFormat | 73.659 ms | 2.204 ms |   2.92 | 249,493,594 | Yes SirRufo      | 74.141 ms | 2.395 ms |   2.64 | 251,712,472 | Yes 

Remarks

JimiNoFormat is just a version using his code with string interpolation

Scale is basically the original array joined and merged that many times, to make a super duper sized array to test

MineOriginal basically just uses a list, and converts it to string right at the end, its kind of more efficient then using a string throughout , however it doesn't scale well and eventually will lose for extremely large datasets

Summary

The recursion ads more overhead, buts its not too bad

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: