Follow up to interview question
My first interview question was pretty successful. The one that was closest to my solution was Paul Bartrum (that name sounds familiar for some reason) followed closely by domovoi (though I didn't test his). Both Paul and I had just a single for loop enumerating once over the collection. I used the concept of "period" which probably has some other name but that's the best description I could come up with. Basically, a period in this sense (and in my mind) is how long it takes a single dimension's index to change when the lower indices are enumerated. This is determined by multiplying the upper bounds of dimensions following that dimension. For example, if you had: int[,,] arr[3,2,3] the third dimension's period is 1 because no dimensions follow it. The second dimension's period is 3 and the 1st dimension's period is 6 (2 x 3). Once I conceptualized that, the rest was trivial. Paul was pretty much thinking along the same lines. There were some good solutions but due to time, I can't go through all of them (especially the python one which threw me for a loop hehe).
Now, here's the next interview question. I think this one will be a lot easier and it follows along the same line. Here goes.
Given a multidimensional array, set each value in that array as efficiently as possible. In other words, don't have a series of nested for loops. Let's say that I will just give you an array without any preconceived notion of how that array was constructed. Make sense? Then get to it!
Comments
- Anonymous
October 07, 2005
"period" = magnitude - Anonymous
October 07, 2005
<pre>
#!/usr/bin/env python
def setValueAtIndex(flatindex, mdArray, value):
indices = ""
for i in getDimensionalIndices(flatindex, mdArray):
indices += "[%d]"%i
exec "mdArray"+indices+"=value"
</pre>
i dunno if i understood correctly
(and i hope indentation is preserved this time) - Anonymous
October 07, 2005
If we can use the previous problem results then it goes like this:
int value;
for (int i=0;i<flatArray.Length;i++)
{ mdArray.setValue(value,GetDimensionalIndices(i,mdArray);
} - Anonymous
October 08, 2005
Hehe, I didn't mention that. Try to create it without using that function that was created previously. Honestly, I haven't had time to try it out yet. Maybe the GetDimensionalIndices is the solution but for some reason I don't think it is. - Anonymous
October 12, 2005
Here's the crude function I used in the previous question to load the initial array values.
It loads the values 1...n for any number of dimensions by building a single dimension array the same length as the multidimensional array, then assigns the values to the multidimesional array using the SetValue method.
-----------------------------------
Private Sub LoadArrayValues(ByRef mdArray As Array)
Dim iArity As Integer = mdArray.Rank - 1
Dim iCnt As Integer = 0
Dim iValue As Integer = 1
Dim arrTemp(iArity) As Integer
Do Until iCnt = iArity
arrTemp(iCnt) = 0
iCnt += 1
Loop
Do Until iValue > mdArray.Length
mdArray.SetValue(iValue, arrTemp)
For iCnt = 0 To iArity
If arrTemp(iArity - iCnt) + 1 <= mdArray.GetUpperBound(iArity - iCnt) Then
arrTemp(iArity - iCnt) += 1
Exit For
Else
arrTemp(iArity - iCnt) = 0
End If
Next
iValue += 1
Loop
End Sub - Anonymous
June 09, 2009
PingBack from http://quickdietsite.info/story.php?id=6190