Bloomberg L.P.

  www.bloomberg.com
  www.bloomberg.com

Interview Question

2013 Financial Software Developer Interview New York, NY

unsorted integer array size n. unmodifiable list of "less

  " / "greater than" operators. place elements from int array in between each "<" and ">" operator so that every comparison holds true. e.g. 6, 2, 8, 1, 3, 9, 4, 0, 5, 7 <, >, <, <, <, >, <, >, > answer: 0 < 9 > 1 < 2 < 3 < 8 > 4 < 7 > 6 > 5
Answer

Interview Answer

2 Answers

0

#include <iostream>

using namespace std;

bool compare(int data[], const char* optrs, const int& sz) {
    int temp;
    for (int i = 0 ; i < sz - 1 ; i++) {
        switch(optrs[i]) {
        case '<':
            if (data[i] > data[i+1]) {
                temp = data[i];
                data[i] = data[i+1];
                data[i+1] = temp;
                return false;
            }
            break;
        case '>':
            if (data[i] < data[i+1]) {
                temp = data[i];
                data[i] = data[i+1];
                data[i+1] = temp;
                return false;
            }
            break;
        }
    }
    return true;
}

int main() {

    const int MAX_DATA_SIZE = 10;
    char optrs[] = "<><<<><>>";
    int data[MAX_DATA_SIZE] = {6,2,8,1,3,9,4,0,5,7};

    while(!compare(data, optrs, MAX_DATA_SIZE)) {
        //Assuming the array data has a solution,
        //Include code here to check for when there is no solution!
    }

    for (int i = 0 ; i < MAX_DATA_SIZE - 1 ; i++)
        cout << data[i] << optrs[i];
    cout << data[MAX_DATA_SIZE -1] << endl;

    return 0;
}

Juan Andrango on Sep 27, 2013
0

Is this the same as a bubble sort, except you need to check each sign, instead of a universal one?

Anonymous on Oct 28, 2013

Add Answers or Comments

To comment on this, Sign In or Sign Up.