How to relocate an element in one array in C++

Viewed 238

I took this interview question and I failed, so I'm here to not fail again!

I have an array of int with size 16 and a 5 < givenIndex < 10.

I have to take the element in this index a print every possible array (there are 16) by moving the element at givenIndex through every position in array and pushing rest of elements.

For example:

int array[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
int givenIndex = 6;

Since array[givenIndex] = 7, I need to move 7 to every possible position and print that array.

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]

[7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16]

[1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16]

[1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16]

And that's for 16 cases.

What I was trying was:

for(int i = 0;i<16;i++){
        array[i] = array[indexInsercion]
        if (i<indexInsert){
            //right shift
            array[i] = array[i+1]
        }else if(i == indexInsert){
            //no shift
        }else{
            //left shift
            array[i] = array[i-1]
        }
    }

Can I get some help?

3 Answers

We can only guess what the interviewer expected to see. If I was the interviewer I would like to see that you keep things simple. This is code I think one can expect to be written from scratch in an interview situation:

#include <iostream>
#include <array>

template <size_t size>
void print_replaced(const std::array<int,size>& x,size_t index){
    for (int i=0;i<size;++i){
        for (int j=0;j<i;++j) {
            if (j == index) continue;
            std::cout << x[j] << " ";
        } 
        std::cout << x[index] << " ";
        for (int j=i;j<size;++j) {
            if (j == index) continue; 
            std::cout << x[j] << " ";
        }
        std::cout << "\n";
    }
}


int main() {
    std::array<int,16> x{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    print_replaced(x,6);
}

It is a first approach at the problem, with a loop that prints 16 different combinations of the array elements. Printing each line follows simple logic: We print all elements before the one that should be replaced, then the one that should be shuffled, then the remaining elements.

It is simple, but wrong. Its output is:

7 1 2 3 4 5 6 8 9 10 11 12 13 14 15 16 
1 7 2 3 4 5 6 8 9 10 11 12 13 14 15 16 
1 2 7 3 4 5 6 8 9 10 11 12 13 14 15 16 
1 2 3 7 4 5 6 8 9 10 11 12 13 14 15 16 
1 2 3 4 7 5 6 8 9 10 11 12 13 14 15 16 
1 2 3 4 5 7 6 8 9 10 11 12 13 14 15 16 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 
1 2 3 4 5 6 8 7 9 10 11 12 13 14 15 16 
1 2 3 4 5 6 8 9 7 10 11 12 13 14 15 16 
1 2 3 4 5 6 8 9 10 7 11 12 13 14 15 16 
1 2 3 4 5 6 8 9 10 11 7 12 13 14 15 16 
1 2 3 4 5 6 8 9 10 11 12 7 13 14 15 16 
1 2 3 4 5 6 8 9 10 11 12 13 7 14 15 16 
1 2 3 4 5 6 8 9 10 11 12 13 14 7 15 16 
1 2 3 4 5 6 8 9 10 11 12 13 14 15 7 16 

There is one line that appears twice and the last line is missing.

As an interviewer I would not be surprised that the first attempt does not produce correct output. I don't care about that. Thats not a minus. What I would care about is how you react on that. Do you know the next steps? Do you have a strategy to fix the wrong output? Or do you just panic because you didn't manage to write the correct code on the first attempt? This is what I would like to check in an interview and then thats the end of the exercise. I want to ask more different questions rather than giving you the time to fix all mistakes and write correct well tested code, because I know that this takes more time than we have in the interview.

I'll leave it to you to fix the above code ;)

Here's a quick stab at it. Basically just keep track of where the given index should go and print it there as well as skip the original position it would be in.

#include <iostream>

int main()
{
    int array[16] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
    int givenIndex = 6;

    for (int p = 0; p <= 16; ++p)
    {
        if (p != givenIndex)
        {
            std::cout << "[";
            for (int i = 0; i < 16; ++i)
            {
                if (i == p)
                {
                    if (i > 0)
                    {
                        std::cout << ",";
                    }
                    std::cout << array[givenIndex];
                }
                if (array[i] != array[givenIndex])
                {
                    if (i > 0 || p == 0)
                    {
                        std::cout << ",";
                    }
                    std::cout << array[i];
                }
            }
            if (p == 16)
            {
                std::cout << "," << array[givenIndex];
            }
            std::cout << "]\n";
        }
    }
}

Output:

[7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16]
[1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16]
[1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16]
[1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16]
[1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16]
[1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
[1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16]
[1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16]
[1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16]
[1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16]
[1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16]
[1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16]
[1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7]

If the expectation is to just print the elements of array in the given order:

Keep the track of current index of array element to be print, say indx -

  • If the position of current element processing is equal to row number then print the element at givenIndex.
  • If indx is equal to givenIndex skip it and print indx + 1 element, otherwise print element at indx and increase indx by 1.

Implementation:

#include <iostream>
#include <array>

int main() {
    std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    std::size_t givenIndex = 6;

    for (std::size_t i = 0, indx = 0; i < array.size(); indx = 0, ++i) {
        std::cout << '[';

        for (std::size_t j = 0; j < array.size(); ++j) {
            if (j == i) {
                std::cout << array[givenIndex] << ',';
                continue;
            }

            if (indx == givenIndex) {
                ++indx;
            }

            std::cout << array[indx++] << ',';
        }

        std::cout << ']';
        std::cout << '\n';
    }

    return 0;
}

Output:

# ./a.out
[7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,]

If the expectation is to alter the order of elements in the array and then print the array:

First move the element at givenIndex to the 0th index of array and then -

  • Print array
  • In every iteration swap the current element with its next element in the array and print it.

Implementation:

#include <iostream>
#include <array>

void print_array (std::array<int, 16>& array) {
    std::cout << '[';
    for (std::size_t indx = 0; indx < array.size(); ++indx) {
        std::cout << array[indx] << ',';
    }
    std::cout << ']';
    std::cout << '\n';
}

void rearrange_array_elem (std::array<int, 16>& array, std::size_t givenIndx) {
    // move the element at givneIndx to first position in array
    for (std::size_t j = givenIndx; j > 0; --j) {
        std::swap (array[j], array[j - 1]);
    }

    // print array
    print_array (array);

    for (std::size_t indx = 0; indx < array.size() - 1; ++indx) {
        // swap current element with its next element
        std::swap (array[indx], array[indx + 1]);
        print_array (array);
    }
}

int main() {
    std::array<int, 16> array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
    std::size_t givenIndex = 6;

    rearrange_array_elem (array, givenIndex);

    return 0;
}

Output:

# ./a.out
[7,1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,7,2,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,7,3,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,7,4,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,7,5,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,7,6,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,7,9,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,7,10,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,7,11,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,7,12,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,7,13,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,7,14,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,7,15,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,7,16,]
[1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,7,]
Related