Apple Interview Question for Software Engineer in Tests


Country: United States




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

Take two pointers. Point out from start. Move one pointer (say p) with one difference and another one (say q) with two difference. So when p is at n/2, q will be on n-1.
eg. n=10.
initially p=q=1.
now p=2 and q=3.
then p=3 and q=5.
then p=4 and q=7.
then p=5 and q=9.
Our search completes when q has no node to move or null.

- Shashank September 13, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

..and there is a general solution for (n-k)th node. start the 2nd pointer k ahead of the 1st pointer. Move them both 1 step each time. when 2nd pointer has no more moves to make, 1st has reached the required node.
In this case, k=1. so p=1, q=2.
then p=2, q=3
...
p=9, q=10.
End.

- romil.bm April 14, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think what you mean by second last (n-1) node is where the * is

*                               n-1             n
_               _               _               _

{
p = ll.head; q == null; i = 0;
while (i < 3 && p != null) {
    p = p.next;
}

if (p != null) {
    q = ll.head;
}
while (p != null) {
  p = p.next;
  q = q.next;
}
//  q is your answer

}

The same principal applies to the second question. q advances half the distance of p in each loop. If p hits the end of the list before it could double its stride, then q advances half the different between end of list and p

- trythis September 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

to get any node from the last :first move a pointer that steps from start and after that take second pointer from start and move both pointer together until first reach at the end ..
now the second pointer points to the desire element from last

- vsingla160 October 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;

class NodeForGetSecondLastAndHalfNode {
    public int data;
    public NodeForGetSecondLastAndHalfNode next;

    public NodeForGetSecondLastAndHalfNode(int iData) {
        data = iData;
    }

    public void display() {
        System.out.print(data + " ==> ");
    }
}

class ListForGetSecondLastAndHalfNode {

    private NodeForGetSecondLastAndHalfNode first;

    public ListForGetSecondLastAndHalfNode() {
        first = null;
    }

    public boolean isEmpty() {
        if (first == null) {
            return true;
        }
        return false;
    }

    public void insertData(int data) {
        System.out.println("INSERT: " + data);
        NodeForGetSecondLastAndHalfNode newElm = new NodeForGetSecondLastAndHalfNode(data);
        newElm.next = first;
        first = newElm;
    }

    public void display() {
        System.out.println("DISPLAY");
        if (isEmpty()) {
            System.out.println("EMPTY");
            return;
        }
        NodeForGetSecondLastAndHalfNode curr = first;
        while (curr != null) {
            curr.display();
            curr = curr.next;
        }
        System.out.println();
    }

    public NodeForGetSecondLastAndHalfNode getFirst() {
        if (isEmpty()) {
            return null;
        }
        return first;
    }
}

class GetSecondLastAndHalfNodeSolution {

    public Object getSecondLastNode(ListForGetSecondLastAndHalfNode list) {
        list.display();
        System.out.print("\t SECOND LAST NODE: ");
        NodeForGetSecondLastAndHalfNode slow = list.getFirst();
        if (list.isEmpty()) {
            return null;
        } else if (slow.next == null) {
            return null;
        }
        NodeForGetSecondLastAndHalfNode fast = slow.next;
        while (fast.next != null) {
            slow = slow.next;
            fast = fast.next;
        }
        System.out.println(slow.data);
        return slow;
    }

    public ArrayList<Integer> getHalfNode(ListForGetSecondLastAndHalfNode list) {
        list.display();
        NodeForGetSecondLastAndHalfNode slow = list.getFirst();
        System.out.print("\t MID NODES: ");
        ArrayList<Integer> out = new ArrayList<Integer>();
        if (list.isEmpty()) {
            return out;
        } else if (slow.next == null || slow.next.next == null) {
            out.add(slow.data);
        } else {
            NodeForGetSecondLastAndHalfNode fast = slow.next;
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
            if (fast != null) {
                out.add(slow.data);
                out.add(slow.next.data);
            } else {
                out.add(slow.data);
            }
        }
        return out;
    }

    public void display(ArrayList<Integer> out) {
        for (int i = 0; i < out.size(); i++) {
            System.out.print(out.get(i) + " , ");
        }
        System.out.println();
    }
}

- Scorpion King January 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

square of n is n times n which is n+n+n+... n-times
so simplest solution for this is ...

int getSqareOfNWithPlusOperator(int n) {
int squareOfN = 0;
for (int I = 0 ; I < n; I++) {
	squareOfN += n;
}

return squareOfN;
}

- Srinivasa Alamuru March 10, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class ListNode(object):
	def __init__(self, x):
		self.val = x
		self.next = None

def search(node, target_distance):
	if node is None:
		return -1, None

	distance, target_node = search(node.next, target_distance)
	if distance + 1 == target_distance:
		target_node = node
	return distance + 1, target_node

if __name__ == '__main__':
	data = '1234567890'

	seed = None
	last_node = None
	for d in data:
		node = ListNode(d)
		if seed is None:
			seed = node
		if last_node is not None:
			last_node.next = node
		last_node = node

	distance, target_node = search(seed, 0)
	print target_node.val

- pointzz.ki August 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None

def search(node, target_distance):
if node is None:
return -1, None

distance, target_node = search(node.next, target_distance)
if distance + 1 == target_distance:
target_node = node
return distance + 1, target_node

if __name__ == '__main__':
data = '1234567890'

seed = None
last_node = None
for d in data:
node = ListNode(d)
if seed is None:
seed = node
if last_node is not None:
last_node.next = node
last_node = node

distance, target_node = search(seed, 0)
print target_node.val

- pointzz.ki August 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public LinkedListNode nthToLastLessComplexity(int n) { //is the value of nth node from last
		if(head== null){
			return null;
		}
		LinkedListNode currentNode = head;
		int length = 0;
		while(currentNode!=null){
			currentNode = currentNode.getNext();
			length ++;
		}
		int m = length;
		int val = m-n+1;
		currentNode = head;
		for(int i=1;i<val;i++){
			currentNode = currentNode.getNext();
		}
	
		return currentNode;
		//complexity is O(n)
	}

- anonymousNg September 03, 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