Interview Question for Software Developers


Country: -




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

/*
step1 : Maintain two counters count1 & count2

count1 contains size of LessThan zigzag order (which starts with <). e,g  a<b>c<d
count2 contains size of GreaterThanzigzag order (which starts with >). e,g  x>y<z>i<j
step2: increase count1 by 1 when you add a new element to LessThan ZigZag order.
increase count2 by 1 when you add a new element to GreaterThan ZigZag order.
Step3: if ith element doesnot meet condition, then  skip it.
*/
int maxZigZagLength(vector<int> &vec)
{
	bool lessThan = true;
	bool greaterThan = true;
	int count1 = 0, count2 = 0;

	for (int i = 1; i < vec.size(); i++)
	{
		if ((lessThan && vec[i - 1] < vec[i]) || (!lessThan && vec[i - 1] > vec[i]))
		{
			lessThan = !lessThan; 
			count1++;
		}

		if ((greaterThan && vec[i - 1] > vec[i]) || (!greaterThan && vec[i - 1] < vec[i]))
		{
			greaterThan = !greaterThan;
			count2++;
		}
	}
	count1++, count2++;
	cout << "Less Than count1 = " << count1 << " , count2 = " << count2 << endl;
	return count1 > count2 ? count1 : count2;
}

int main()
{
	vector<int> vec = { 1,42,59,43,68,44 }; // answer is 1 < 42 > 43< 68 > 44
	cout << "lenght = " << maxZigZagLength(vec) << endl; 

	vector<int> vec1 = { 100,42,54,2,5 }; // answer is  100> 42 <54>2 < 5
	cout << "lenght = " << maxZigZagLength(vec1) << endl;

	vector<int> vec2 = { 1,42,54,20,1 }; // answer is 1<42>20 or 42 < 54 > 20
	cout << "lenght = " << maxZigZagLength(vec2) << endl;
	getchar();
}

Please let me know if you find any bugs in approach

- siva.sai.2020 November 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take the input as Integer Array.
Store the position where the first greater than or less than occurs (keep skipping if the elements are equal). Store the comparison value i.e. 1 or -1 in first
From that point on, find the next less than, greater than (next in this case) and compare it the last stored i.e first in this case.
At then end, the number of less thans and greater thans are found. Return result+!

public class ZigZagArrayMaxDemo {

    public static int maxZigZagLength(Integer[] input) {
        if(input.length ==0 ) throw new IllegalArgumentException("array length is 0");
        int result = 0;

        int first = 0;
        int startIndex = 0;
        for(int i = 0; i < input.length - 1; i++) {
            first = input[i].compareTo(input[i+1]);
            if(first !=0) {
                startIndex = i+1;
                result = 1;
                break;
            }
        }

        if(first == 0) return 0;
        int next;
        for (int i = startIndex; i < input.length - 1; i++) {
            next = input[i].compareTo(input[i+1]);
            if(next == 0) continue;

            if((next != first)) {
                result++;
                first = next;
            }

        }


        return result+1;
    }

    public static void main(String[] args) {
        Integer[] inputArray = {1,42,54,20,1 };
        System.out.println(maxZigZagLength(inputArray));
    }
}

- vicky17D November 24, 2015 | 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