## Interactive Brokers Interview Question for Software Engineer / Developers

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

Sort the vector first, and then looking fo the longest subsequence?
The solution works, but not the one that interviwer expects...

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

you will endup printing entire array because a sorted array is always either increasing or decreasing.

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

suppose length[i] is the length of the longest increasing subsequence ending at index i.

length[0] = 1
for all index from 1 to n-1
if array[i] > array[i-1] then length[i] = length[i-1]+1 because we are extending the sequence ended at index (i-1) by 1
else length[i] = 1 because we are starting new sequence from ith index.

find greatest length[i] for all i and is the answer required. similarly can be done for decreasing subsequence. time O(n)

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

nice1

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

public static void findArray(int[] array){

int maxStartingPos = -1;
int tempMaxStartingPos = -1;
int maxLength = -1;
int tempMaxLength = -1;

for(int i = 0; i < array.length-1; i++){
int fist = array[i];
int second = array[i+1];
if(fist<=second){
if( (tempMaxStartingPos+tempMaxLength)==i){
tempMaxLength++;
}else{
tempMaxStartingPos = i;
tempMaxLength = 1;
}
}else{
if(maxLength<tempMaxLength){
maxLength=tempMaxLength;
maxStartingPos = tempMaxStartingPos;
}
}
}

System.out.println("Max Elements");
for(int i = maxStartingPos; i<=maxStartingPos+maxLength;i++){
System.out.print(array[i]);
}
}

This works for ascending

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

Nice! When did c++ get System.out?

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

void findSubsequence(int [] A, int n)
{
// A is the input array
// n is the size of array
int maxAsc = 0;
int maxDsc = 0;
int indexAsc = 0;
int indexDsc = 0
int maxLenAsc = 0;
int maxLenDsc = 0;

if (n == 0) std::cout<<"empty";
if(n == 1) std::cout<< A[0];

for(int i = 1; i < n ; i++)
{
if(A[i] < A[i-1])
{
max = A[i];
maxDsc++;
if (maxLenDsc < maxDsc)
{
indexDsc = i;
}

}
else
{
maxLenDsc = maxDsc;
maxDsc = 0;
}
if(A[i] > A[i-1])
{
max = A[i];
maxAsc++;
if (maxLenAsc < maxAsc)
{
maxLenAsc = maxAsc;
indexAsc = i;

}

}
else
{
maxAsc = 0;
}
}
if (maxLenAsc > maxLenDsc)
{
for(int i = (indexAsc - maxLenAsc); i < indexAsc ; i ++)
{
std::cout<<A[i];
}
else
{
for(int i = (indexDsc - maxLenDsc); i < indexDsc ; i ++)
{
std::cout<<A[i];
}
}
}
}

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

``````#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int main(){
vector<int> numbers;
int n, num, i;
int start_index, end_index, max = 0, count = 0;

//User Input
cin>>n;
for( i = 0 ; i<n ; i++) {
cin>>num;
numbers.push_back(num);
}

//--------For Ascending---------------------------------------------------------------------------
for( i = 0 ; i<n ; i++){
if(numbers[i] == numbers[i - 1] + 1){
count++;
if(max<count){
start_index = i - count;
max = count;
end_index = i;
}
}
else{
count = 0;
}
}

cout<<"---------------------------------------\n";
cout<<"Longest Ascending Sequence:\n";
cout<<"Length: "<<max + 1<<"\n";
cout<<"Start index:"<<start_index<<", End Index:"<<end_index<<"\n";
for(i = start_index ; i<=end_index ; i++){
cout<<numbers[i]<<" ";
}
cout<<"\n";

//--------For Descending--------------------------------------------------------------------------
count = 0;
max = 0;
start_index = 0;
end_index = 0;
for( i = 0 ; i<n ; i++){
if(numbers[i] == numbers[i - 1] - 1){
count++;
if(max<count){
start_index = i - count;
max = count;
end_index = i;
}
}
else{
count = 0;
}
}

cout<<"---------------------------------------\n";
cout<<"Longest Descending Sequence:\n";
cout<<"Length: "<<max + 1<<"\n";
cout<<"Start index: "<<start_index<<", End Index: "<<end_index<<"\n";
for(i = start_index ; i<=end_index ; i++){
cout<<numbers[i]<<" ";
}
cout<<"\n";
cout<<"---------------------------------------\n";

return 0;``````

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

``````#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
int main(){
vector<int> numbers;
int n, num, i;
int start_index, end_index, max = 0, count = 0;

//User Input
cin>>n;
for( i = 0 ; i<n ; i++) {
cin>>num;
numbers.push_back(num);
}

//--------For Ascending---------------------------------------------------------------------------
for( i = 0 ; i<n ; i++){
if(numbers[i] == numbers[i - 1] + 1){
count++;
if(max<count){
start_index = i - count;
max = count;
end_index = i;
}
}
else{
count = 0;
}
}

cout<<"---------------------------------------\n";
cout<<"Longest Ascending Sequence:\n";
cout<<"Length: "<<max + 1<<"\n";
cout<<"Start index:"<<start_index<<", End Index:"<<end_index<<"\n";
for(i = start_index ; i<=end_index ; i++){
cout<<numbers[i]<<" ";
}
cout<<"\n";

//--------For Descending--------------------------------------------------------------------------
count = 0;
max = 0;
start_index = 0;
end_index = 0;
for( i = 0 ; i<n ; i++){
if(numbers[i] == numbers[i - 1] - 1){
count++;
if(max<count){
start_index = i - count;
max = count;
end_index = i;
}
}
else{
count = 0;
}
}

cout<<"---------------------------------------\n";
cout<<"Longest Descending Sequence:\n";
cout<<"Length: "<<max + 1<<"\n";
cout<<"Start index: "<<start_index<<", End Index: "<<end_index<<"\n";
for(i = start_index ; i<=end_index ; i++){
cout<<numbers[i]<<" ";
}
cout<<"\n";
cout<<"---------------------------------------\n";

return 0;``````

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

This is in Java but it work

``````void printLongestIncSub(int a[])
{
int max = -1;
int c = 0;
int end = 0;
int strt=0;

for (int i = 1; i < a.length; i++) {
if (a[i - 1] < a[i]) {

c++;

if (max < c) {
max = c;
end = i;
strt=end-max;

}
} else {

c = 0;

}

}
System.out.println("Longest Ascending Subsequence");
for(int i=strt;i<=end;i++)
{
System.out.print(a[i]+" ");
}
}``````

Add a Comment
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.

Learn More

### 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.

Learn More

### Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

### Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More