Brocade Interview Question for Software Trainees


Country: United States




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

The idea is to use Merge function of Merge sort.

Create an array arr3[] of size n1 + n2.
Simultaneously traverse arr1[] and arr2[].
Pick smaller of current elements in arr1[] and arr2[], copy this smaller element to next position in arr3[] and move ahead in arr3[] and the array whose element is picked.
If there are remaining elements in arr1[] or arr2[], copy them also in arr3[].

- Nits January 30, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is how I'd solve that, considering arrays a and b to merge to c

check one element from a and b and add the min to c until one of the arrays runs out of elements

add remaining elements from a or b to c

fun main() {
    val a = arrayOf(1, 3, 5, 7, 8, 9)
    val b = arrayOf(2, 4, 6)
    print("A = ${a.asList()}")
    print("B = ${b.asList()}")
    
    val c = Array(a.size + b.size) { 0 }
    
    var i = 0
    var j = 0
    var k = 0
    // find the min of a[j],b[j] and add it at c[k]
    // until one of the arrays(a or b) is empty
    while (i < a.size && j < b.size) {
        if (a[i] < b[j]) {
            c[k] = a[i]
            i++
            k++
        } else {
            c[k] = b[j]
            j++
            k++
        }
    }
    
    // add remaining elements to c
    while (i < a.size) {
        c[k] = a[i]
        i++
        k++
    }
    while (j < b.size) {
        c[k] = b[j]
        j++
        k++
    }
    
    print("C = ${c.asList()}")
}

note: i contains position of current element in A similarly j for B and k for C

- PeyarTheriyaa January 30, 2020 | 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