Sap Labs Interview Question for Developer Program Engineers


Team: Potsdam
Country: United States
Interview Type: Phone Interview




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

#include<conio.h>
main{
}

- Anonymous October 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;

/**
 *
 * @author Uday Kandpal
 */

/*
 * An interface for an sorted binary tree.
 * 
 * The interface provides methods for inserting values, checking if certain values are contained and iterating over the elements.
 * Note: Implementing classes should provide an iterator that traverse the inserted object in the sorted order.
 */
interface IBinTree<V extends Comparable<V>> extends Iterable<V> {
    /*
     * Insert an object into the binary tree. Note: The tree should be sorted, inserting the same object twice is allowed but the insert is expected to be stable.
     */

    void insert(V obj);
    /*
     * Batch-insert multiple elements.
     */

    void insert(Vector<V> vec);
    /*
     * Check if the object is already in the tree. Return true if it is, false otherwise. 
     */

    boolean contains(V obj);
}

public class BinaryTree<V extends Comparable<V>> implements IBinTree<V> {

    V value = null;
    BinaryTree<V> left = null;
    BinaryTree<V> right = null;

    private Vector<V> preOrder() {
        Vector<V> vector = new Vector();
        if (left != null) {
            vector.addAll(left.preOrder());
        }
        vector.add(value);
        if (right != null) {
            vector.addAll(right.preOrder());
        }
        return vector;
    }

    @Override
    public void insert(V obj) {
        if (value == null) {
            value = obj;
        } else if (value.compareTo(obj) == 1) {
            if (left == null) {
                left = new BinaryTree<>();
            }
            left.insert(obj);
        } else {
            if (right == null) {
                right = new BinaryTree<>();
            }
            right.insert(obj);
        }
    }

    @Override
    public void insert(Vector<V> vec) {
        Iterator<V> it = vec.iterator();
        while (it.hasNext()) {
            insert(it.next());
        }
    }

    @Override
    public boolean contains(V obj) {
        if (value.compareTo(obj) == 0) {
            return true;
        } else if (value.compareTo(obj) == 1) {
            return (left != null && left.contains(obj));
        } else {
            return (right != null && right.contains(obj));
        }
    }

    @Override
    public Iterator<V> iterator() {
        return preOrder().iterator();
    }

    public static void main(String[] args) {
        BinaryTree<Integer> tree = new BinaryTree<>();
        Integer[] list = new Integer[]{5, 3, 2, 8, 1, 7, 4, 9};
        tree.insert(new Vector<>(Arrays.asList(list)));
        Iterator it = tree.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }

    }
}

- Uday Kandpal November 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for the mistake, change the name of the function preOrder() to inOrder()

- Uday Kandpal November 01, 2014 | Flag


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