Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

void editArray(int arr[], int m, int arr_size)
{
	int cur, i, cur_ele = -1;
	int count = 0;
    
	cur = 0;
	
	for(int i = 0;i < arr_size;i++)
	{
		if (cur_ele != arr[i])
		{
			cur_ele = arr[i];
			count = 0;
		}

		if (cur_ele == arr[i] && count < min(m, 2))
		{
            		count++;
            		arr[cur] = arr[i];
            		cur++;
        	}
    	}
    
    	for(int i = cur;i < arr_size;i++)
        	arr[i] = 0;
}

- B_K_J April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

can you add an example?

- Marcello Ghali April 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if an array is {2,2,2,2,3} and the method is editArray(arr[],m)
if m=4
then after the method array should be {2,2,3,0,0}

- ashishsaraswat.iips April 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
	 * 
	 * @param input Array of positive numbers in the sorted order.
	 * @param x 
	 */
	private static void editArray(int[] input, int x) {

		int lCount = 0;
		for (int i = 0, j = -1, k = 0; i < input.length; i++) {

			if (input[i] == x) {

				lCount++;
				if (lCount > 2) {
					if (j == -1) {
						j = i;
					}
					input[i] = 0;
				}
			} else if (lCount > 0) {

				if (j != -1) {
					k = j;
					j = -1;
				}
				input[k++] = input[i];
				input[i] = 0;
			}

		}
	}

- Anonymous April 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
	 * 
	 * @param input Array of positive numbers in the sorted order.
	 * @param x 
	 */
	private static void editArray(int[] input, int x) {

		int lCount = 0;
		for (int i = 0, j = -1, k = 0; i < input.length; i++) {

			if (input[i] == x) {

				lCount++;
				if (lCount > 2) {
					if (j == -1) {
						j = i;
					}
					input[i] = 0;
				}
			} else if (lCount > 0) {

				if (j != -1) {
					k = j;
					j = -1;
				}
				input[k++] = input[i];
				input[i] = 0;
			}

		}
	}

- santhana April 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
	 * 
	 * @param input Array of positive numbers in the sorted order.
	 * @param x 
	 */
	private static void editArray(int[] input, int x) {

		int lCount = 0;
		for (int i = 0, j = -1, k = 0; i < input.length; i++) {

			if (input[i] == x) {

				lCount++;
				if (lCount > 2) {
					if (j == -1) {
						j = i;
					}
					input[i] = 0;
				}
			} else if (lCount > 0) {

				if (j != -1) {
					k = j;
					j = -1;
				}
				input[k++] = input[i];
				input[i] = 0;
			}

		}
	}

- santhanasamy April 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
	 * 
	 * @param input Array of positive numbers in the sorted order.
	 * @param x 
	 */
	private static void editArray(int[] input, int x) {

		int lCount = 0;
		for (int i = 0, j = -1, k = 0; i < input.length; i++) {

			if (input[i] == x) {

				lCount++;
				if (lCount > 2) {
					if (j == -1) {
						j = i;
					}
					input[i] = 0;
				}
			} else if (lCount > 0) {

				if (j != -1) {
					k = j;
					j = -1;
				}
				input[k++] = input[i];
				input[i] = 0;
			}

		}
	}

- Santhana April 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
	 * 
	 * @param input Array of positive numbers in the sorted order.
	 * @param x 
	 */
	private static void editArray(int[] input, int x) {

		int lCount = 0;
		for (int i = 0, j = -1, k = 0; i < input.length; i++) {

			if (input[i] == x) {

				lCount++;
				if (lCount > 2) {
					if (j == -1) {
						j = i;
					}
					input[i] = 0;
				}
			} else if (lCount > 0) {

				if (j != -1) {
					k = j;
					j = -1;
				}
				input[k++] = input[i];
				input[i] = 0;
			}

		}
	}

- a.santhanasamy April 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def editArray(a, m):

    cur_el = None
    cur_el_count = 0
    cur = 0
    
    for i in range(0, len(a)):
        if a[i] != cur_el:
            cur_el_count = 0
            cur_el = a[i]

        if a[i] == cur_el:
            cur_el_count += 1

            if cur_el_count <= min(m, 2):
                a[cur] = a[i]
                cur += 1

    for i in range(cur, len(a)):
        a[i] = 0

- gaurav414u May 04, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Javascript solution

var arr = [1,2,2,2,2,3,4];
var m = 4;
var cur = 0, i, currElement = -1;
var count = 0;

console.log(arr)
for(var i = 0;i < arr.length;i++)
{

  // set the current element
  if (currElement != arr[i])
  {
    currElement = arr[i];
    count = 0;
  }

  // the moment count is > than min, it wil stop incrementing the "curr" pointer
  // and wait for next condition to match
  // so that subsequent filling will resume and start removing the duplicates.
  if (currElement == arr[i] && count < Math.min(m, 2))
  {
    count++;
    // start filling the array with matching value
    arr[cur] = arr[i];
    cur++;
  }
}

for(var i = cur;i < arr.length;i++) {
  arr[i] = 0;
}
console.log(arr)

- rocks May 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Java Solution

public static void editArray(int[] arr, int m) {
		int curElm = 0, curIndx = 0, count = 0;
		for(int i = 0; i < arr.length; i++) {
			if(curElm == arr[i]) {
				count++;
			} else count = 1;
			
			if(count <= Math.min(2,  m)) {
				arr[curIndx] = arr[i];
				curElm = arr[i];
				curIndx++;
			}
		}
		
		while(curIndx < arr.length) {
			arr[curIndx] = 0;
			curIndx++;
		}
	}

- abhi10901 May 27, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void editArray(vector<int>& arr, int m)
{
	auto p0 = arr.begin();
	auto p1 = arr.begin();
	while (p1 != arr.end())
	{
		auto er = equal_range(p1, arr.end(), *p1);
		auto p2 = er.second;
		int sz = distance(p1, p2);
		if (sz == m)
			sz = min(m, 2);

		p0 = copy(p1, p1 + sz, p0);
		p1 = p2;
	}

	arr.erase(p0, arr.end());
}

- john700 June 05, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void EditArray(int[] arr, int x)
        {
            int lCount = 0;
            for (int i = 0, j = -1, k = 0; i < arr.Length; i++)
            {
                if (arr[i] == x)
                {
                    lCount++;
                    if (lCount > 2)
                    {
                        if (j == -1)//Set the position of i starting where lCount>2, i.e. we are going to assign the 0 to arr[i]
                        {
                            j = i;
                        }
                        arr[i] = 0;
                    }
                }
                else if (lCount > 0)//if x is found more than x
                {
                    if (j != -1)//if j has been assigned the value of i, i.e. position where and on we have set arr[i] to 0  
                    {
                        k = j;
                        j = -1;
                    }
                    if (k > 0)
                    {
                        arr[k++] = arr[i];
                        arr[i] = 0;
                    }
                }
            }

}

- Manishtiwari2k5 June 17, 2017 | 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