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>();

            currentSeq.Add(sequence[0]);
            int lastValue = sequence[0];

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

                    currentSeq = new List<int>();
                    currentSeq.Add(sequence[i]);
                }

                lastValue = sequence[i];
            }

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

- Rich August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the array and get the sequence!

- Varun August 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

:|
Sorting it will change the sequence :\

- Anindya August 21, 2013 | Flag
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>();

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

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

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

        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 + " ");
        }
    }
}

- trish August 17, 2013 | Flag Reply
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

- gowin August 18, 2013 | Flag Reply
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).

- Anonymous August 18, 2013 | Flag Reply
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

- Gore August 21, 2013 | Flag Reply
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.

- bigphatkdawg September 19, 2013 | Flag Reply
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;

}

- sakthivel Sethu,Software Engineer ,Bangalore October 15, 2013 | Flag Reply


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