## Interview Question

Country: United States

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

``````static public void LongSequence(int[] sequence)
{
if (sequence == null || sequence.Length == 0) return;

Array.Sort(sequence);

int[] longSeq = null;
List<int> currentSeq = new List<int>();

int lastValue = sequence[0];

for (int i = 1; i < sequence.Length; i++)
{
// Is the sequence intact?
if (lastValue + 1 == sequence[i])
{
}
else
{
if (longSeq == null || currentSeq.Count > longSeq.Length)
{
longSeq = currentSeq.ToArray<int>();
}

currentSeq = new List<int>();
}

lastValue = sequence[i];
}

if (longSeq == null || currentSeq.Count > longSeq.Length)
{
longSeq = currentSeq.ToArray<int>();
}
}``````

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

Sort the array and get the sequence!

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

:|
Sorting it will change the sequence :\

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

``````public class LongSequenceTest {

public static ArrayList<Integer> LongSequence(Integer... sequence) {
if (sequence == null || sequence.length == 0)
return null;

List<Integer> seqList =  new ArrayList<Integer>(Arrays.asList(sequence));
Collections.sort(seqList);

List<Integer> currentSeq = new ArrayList<Integer>();
List<Integer> retSeq = new ArrayList<Integer>();

int lastValue = seqList.get(0);
int longSeq = 0;

for(int i=1; i < seqList.size(); i++){

if(lastValue+1 == seqList.get(i)){
lastValue++;
} else {
if(longSeq == 0 || currentSeq.size() > longSeq){
longSeq = currentSeq.size();
retSeq = currentSeq;
}
lastValue = seqList.get(i);
currentSeq = new ArrayList<Integer>();
}
}

if (longSeq == 0 || currentSeq.size() > longSeq){
retSeq = currentSeq;
}

return (ArrayList<Integer>) retSeq;
}

public static void main(String arg[]) {
ArrayList<Integer> ret = LongSequence(7, 2, 3, 5, 6, 9);

for(Integer i : ret) {
System.out.print(i + " ");
}
}
}``````

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

if space complexity is not the issue ,u can use Hashset . First insert all the element then for each element search its next or previous if anyone present .If any one present delete both from the HashSet and increase the counter accordingly and search for next element .For each new element make count=0,also make track of max count .complexity O(n) both in space and time

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

Another approach is: We can construct an un-directed graph where nodes are the array elements and there is an edge between two nodes iff their values differ with one. Then we can run the DFS and choose the longest connected component. Complexity is O(V + E).

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

Insert by direct hashing, then

getLongestSeq(i){
if(i.hasNext)
if(i.noPrev)
store i;
i.count++;
else
getCurrentI
currentI.count++;
getLongestSeq(i.next)
else
find next elem , recurssively run till null

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

Find the longest sequence in the array.

array = {1, 4, 3, 2, 5, 7, 8, 22}
Answer = 1, 2, 3, 4, 5

What? What does this mean?
Why isn't the whole array the answer?
Longest (sub)sequence of any sequence is the whole sequence.

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

``````#include<stdio.h>
#include<stdlib.h>

void sort_array(int arr[], int n)
{
int i, j, temp;

for (i = 0 ; i < ( n - 1 ); i++)
{
for (j = 0 ; j <( n - i - 1); j++)
{
if (arr[j] > arr[j+1])
{
/* Swapping */

temp  = arr[j];
arr[j]   = arr[j+1];
arr[j+1] = temp;
}
}
}
}

int main()
{
int i;
int arr[] = {1,4, 3, 2, 45, 7, 48, 66} ;
sort_array(arr,7);
printf("%d ",arr[0]);
for(i=1;i<sizeof(arr)/sizeof(int);i++)
{
if( (arr[i] - arr[i-1]) == 1)
printf("%d ",arr[i]);
else
break;
}

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.