Welcome to MSDN Blogs Sign in | Join | Help

Mark Schmidt's Abode

On Programming, Writing, Gaming, Fitness, Life

News

  • Have a Question?

    Click here to chat with me

    XBox Live GamerTag


    Twitter



    The Mark Cam


    My MoBlog

    www.flickr.com
    This is a Flickr badge showing public photos from codepunk. Make your own badge here.

    Community-Credit

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!
Posted: Friday, October 07, 2005 11:26 AM by markhsch

Comments

Will Sossamon said:

"period" = magnitude
# October 7, 2005 4:34 PM

sy said:

<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)
# October 7, 2005 9:53 PM

Panagiotis Grontas said:

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);
}

# October 8, 2005 3:09 AM

markhsch said:

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.
# October 8, 2005 11:23 AM

Brant Jones said:

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
# October 12, 2005 12:41 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker