views:

75

answers:

4

In a recent interview one peculiar question has been asked

a[]= { 1,2,3,4,5,6,7,8,9,10}

When an array is given with specified starting index i have to iterate it till i traverse all elements.

I mean suppose the starting index is "5" i have to start from 6,7,8,9,10,5,4,3,2,1.Please carefully look at the sequence ,how can one create Reset(), Current,blah,blah...?.

But the interviewer executed the sequence as he asked.He did not show the code.

Can we really develop a logic for such a strange requirement?

A: 

Yes, you must implement the IEnumerator interface on a custom class. Write logic into the MoveNext method that detects end of array and then starts at the beginning of the array, remembering the mid-way start point.

Follow these code samples by Microsoft as a template.

John K
+5  A: 

Just use two loops.

IEnumerable<int> ForwardsBackwards(int[] a, int start) {
    for(int i=start; i<a.Length; i++) 
        yield return a[i];
    for(int i=start-1; i>=0; i--) 
        yield return a[i];
}

Edit: Even better with linq:

IEnumerable<int> ForwardBackward(int[] a, int start) {
    return a.Skip(start).Concat(a.Take(start).Reverse());
}
recursive
oh! sorry i thought it as invalid question.we can achieve it practically.
udana
Why would it be invalid?
recursive
i thought the interviewer asked nasty question
udana
I would argue that the "better" LINQ version isn't really that much better, since it iterates over the array twice (IIRC, neither `Skip` nor `Take` implementations check for `IList` to optimize).
Pavel Minaev
If that's a nasty question, I need to go do some interviews.
recursive
A: 

Easier to maintain(?):

        int[] a= {1,2,3,4,5,6,7,8,9,10};

        int StartIndex=5;

        for (int iCount = StartIndex; iCount < a.Count() + StartIndex; iCount++)
        {
            Debug.WriteLine(a[(iCount + a.Count()) % a.Count()]);
        }

Output is:

6 7 8 9 10 1 2 3 4 5

Edit: just spotted your sequence, it reverses when you reach the upper limit. That's nasty if that was the specified question.

Neil Kimber
Without mentioning the fact that the output is wrong, which you already noticed, the + a.Count() inside the index is redunant, since you then perform % a.Count().
recursive
A: 
(DEFINE (traverse length start call) 
  (DEFINE (flow index motion)
    (cond ((> index length) 
              (flow (- start 1) -1))
          ((> index -1) 
              ((call index) (flow (+ index motion) motion)))))
  (flow start +1))

(traverse 9 5 job)

I like the 9 to 5 aspect of the question. I wonder if they were alluding to something? Assuming job prints out the array index, this would produce 678954321.

In C i'd be tempted to use a for loop (not a while loop as I had it before the edit);

int a[]= { 1,2,3,4,5,6,7,8,9,10};
int length = 10;
int start = 5;
int motion = 1; //increment

for( int index = start; index >= 0; index += motion ) {
  if(index > (length-1)) {
    motion = -1; //decrement
    index = start -1;
  else {
    printf("%d", a[index]);
  }
}

The process is only finished after you print the zero index. The only time you're not traversing the array is when you've exceeded it. When that happens, you return to the start index (-1) and reverse the motion.

deau