Write a function (in pseudo-code) called dumpList that takes as its parameters a string and a reference to an arbitrarily complex nested list and prints the value of each list element on a separate line. The value of each line should be preceded by the string and numbers indicating the depth and index of the element in the list. Assume that the list contains only strings and other nested lists.
For example, given the following nested list:

List = ['a string', ['a','b','c'], 'spam', ['eggs']] 
and the string 'Foo', 
the output of dumpList('Foo', List) would look like:

Foo.0:  a string
Foo.1.0: a
Foo.1.1 : b
Foo.1.2: c
Foo.2: spam
Foo.3.0: eggs

The function should be less than 20 lines of pseudo-code.
					

This question was asked in a Google interview for a software engineering position.

It's obviously important to thoroughly understand any question before you answer it, so that you don't miss anything. With that in mind, let's make sure that we clarify what is being asked here. The main thing to take note of in this question is the part that says "arbirtrarily complex nested list". What that means is that the function we are being asked to write should be able to handle a list that can be as deep as the creator of the list would like it to be. In the example given above, we have a list that is only 2 levels deep, but you could potentially have something that's 100 or even 10000 levels deep - so, our function should be able to handle any depth within the list.

Now that an important point has been clarified, we should come up with a sound strategy to solve this problem. Take a close look at the question - it uses words like 'nested' and 'depth'. Words like these should generally get you to start thinking recursively.

Because the list can contain other lists as elements, we need to be sure to check to see if each element is a list. If the element is a list then we want to make a recursive call, passing in a new string with the current depth of the list. If the element is not a list then we want to print out the current string and then the element itself.

Here's what the function would look like in pseudo code:

						
dumpList(String x, &list)
{
	 for(int y = 0; y < list.length(); y++)
	 {
		  if(list[y] is a list)
			dumpList(x.y, list[y]);
			
		  else
		   print "x.y:  ", list[y];
	 }
}