## Stripe Interview Question for Software Engineers

Country: United States

Comment hidden because of low score. Click to expand.
0
of 0 vote

void MergeSort<T>(T[] array, params Func<T, IComparable>[] sortOrder)
{
MergeSort(array, 0, array.Length - 1, sortOrder);
}

void MergeSort<T>(T[] array, int start, int end, Func<T, IComparable>[] sortOrder)
{
//divide in 2 (if possible)
int mid = (start + end) / 2;

//sort left
if (mid > start)                  //mid = 2, start = 1, will merge
MergeSort(array, start, mid, sortOrder);

//sort right
if (end - mid >= 2)
MergeSort(array, mid + 1, end, sortOrder); //end = 9, mid = 7, will sort 8 to 9

//merge left and right!
Merge(array, start, mid, end, sortOrder);
}

void Merge<T>(T[] array, int start, int mid, int end, Func<T, IComparable>[] sortOrder)
{
//create aux variables
T[] auxArray = new T[end - start + 1];
int auxArrayIdx = 0;
int leftIdx = start;
int rightIdx = mid+1;

for (int i = start; i <= end; i++)  //or we can do a while... same thing, i'm not using the i
{
//i finished with the left, it's all right
if (leftIdx > mid)
{
auxArray[auxArrayIdx] = array[rightIdx];
rightIdx++;
}
//finished with right, take left
else if (rightIdx > end)
{
auxArray[auxArrayIdx] = array[leftIdx];
leftIdx++;
}
//left smaller than right
else if (Compare(array[leftIdx], array[rightIdx], sortOrder) < 0)
{
auxArray[auxArrayIdx] = array[leftIdx];
leftIdx++;
}
//right smaller than left
else
{
auxArray[auxArrayIdx] = array[rightIdx];
rightIdx++;
}

auxArrayIdx++;
}

for (int i = 0; i < auxArray.Length; i++)
{
array[i + start] = auxArray[i];
}

}

private int Compare<T>(T t1, T t2, Func<T, IComparable>[] sortOrder)
{
for (int i = 0; i < sortOrder.Length; i++)
{
IComparable value1 = sortOrder[0](t1);
IComparable value2 = sortOrder[0](t2);

int comp = value1.CompareTo(value2);

if (comp != 0)
return comp;
}
return 0;
}

Comment hidden because of low score. Click to expand.
0
of 0 vote

void MergeSort<T>(T[] array, params Func<T, IComparable>[] conditions) {
if(conditions.Length == 0)
conditions = new Func<T, IComparable>[] {
(x) => x as IComparable
};
MergeSort(array, 0, array.Length-1, conditions);
}

void MergeSort<T> (T[] array, int start, int end, Func<T, IComparable>[] conditions){
int mid = (start + end) / 2;

//partition / Sort each part
if (start < mid)
MergeSort(array, start, mid, conditions);

if (mid+1 < end)
MergeSort(array, mid+1, end, conditions);

//Merge
Merge(array, start, mid, end, conditions)
}

void Merge(T[] array, int start, int mid, int end, Func<T, IComparable>[] conditions){
int chunkLength = end - start + 1;
T[] auxArray = new T[chunkLength];

int idxLeft = start;
int idxRight = mid+1;

//fill the aux array
for(int i = 0; i < chunkLength; i++){
if(idxLeft > mid) {
//take right
auxArray[i] = array[idxRight++];
}
else if (idxRight > end) {
//take left
auxArray[i] = array[idxLeft++];
}
else if(Compare(array[idxLeft], array[idxRight]) > 0){
//take left
auxArray[i] = array[idxLeft++];
}
else{
//take right
auxArray[i] = array[idxRight++];
}
}

//override the original array
for(int i = 0; i < chunkLength; i++){
array[i+start] = auxArray[i];
}
}

int Compare<T>(T t1, T t2, Func<T, IComparable>[] conditions) {
for(int i = 0; i < conditions.Length; i++){
IComparable v1 = conditions[i](t1);
IComparable v2 = conditions[i](t2);

if (v1 != null && v2 != null){
int value = v1.CompareTo(v2);

if(value != 0)
return value;
}
}
return 0;

}

Comment hidden because of low score. Click to expand.
0
of 0 vote

void MergeSort<T>(T[] array, params Func<T, IComparable>[] conditions) {
if(conditions.Length == 0)
conditions = new Func<T, IComparable>[] {
(x) => x as IComparable
};
MergeSort(array, 0, array.Length-1, conditions);
}

void MergeSort<T> (T[] array, int start, int end, Func<T, IComparable>[] conditions){
int mid = (start + end) / 2;

//partition / Sort each part
if (start < mid)
MergeSort(array, start, mid, conditions);

if (mid+1 < end)
MergeSort(array, mid+1, end, conditions);

//Merge
Merge(array, start, mid, end, conditions)
}

void Merge(T[] array, int start, int mid, int end, Func<T, IComparable>[] conditions){
int chunkLength = end - start + 1;
T[] auxArray = new T[chunkLength];

int idxLeft = start;
int idxRight = mid+1;

//fill the aux array
for(int i = 0; i < chunkLength; i++){
if(idxLeft > mid) {
//take right
auxArray[i] = array[idxRight++];
}
else if (idxRight > end) {
//take left
auxArray[i] = array[idxLeft++];
}
else if(Compare(array[idxLeft], array[idxRight]) > 0){
//take left
auxArray[i] = array[idxLeft++];
}
else{
//take right
auxArray[i] = array[idxRight++];
}
}

//override the original array
for(int i = 0; i < chunkLength; i++){
array[i+start] = auxArray[i];
}
}

int Compare<T>(T t1, T t2, Func<T, IComparable>[] conditions) {
for(int i = 0; i < conditions.Length; i++){
IComparable v1 = conditions[i](t1);
IComparable v2 = conditions[i](t2);

if (v1 != null && v2 != null){
int value = v1.CompareTo(v2);

if(value != 0)
return value;
}
}
return 0;
}

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.