Salesforce Interview Question for Interns


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




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

The idea is to just find the right position to insert by keeping two pointers and then either insert or swap values as needed

public class SortedCircularList
{
  //sentinel node.
  private final Node head;
  
  public SortedCircularList()
  {
  	head = new Node(null);
  	head.next = head;
  }
  
  public static void main(String ...args)
  {
     SortedCircularList list = new SortedCircularList();
     list.add(new Node(4));
     list.add(new Node(10));
     list.add(new Node(14));
     list.print();

     
     //Adding a node in between.
     list.add(new Node(0));
     list.print();
     list.add(new Node(11));
     list.print();     
  }
  
  
  public void add(Node n)
  {
     if(head.next==head)
     {
     	head.next = n;
     	n.next = head;
     	return;
     }
     
     //find the right pos.
     Node prev = head.next;
     Node cur = head.next.next;
     while(cur!=head && cur.data<=n.data)
     {
     	prev = cur;
		cur = cur.next;
     }
     
     n.next = prev.next;
     prev.next = n;
     if(n.data<prev.data)
     {
        int temp = n.data;
        n.data = prev.data;
        prev.data = temp;
     }     
  }
  
  
  
  
  
  public void print()
  {
     Node n = head.next;
     while(n!=head)
     {
     	System.out.println(n.data);
     	n = n.next;
     }
     System.out.println("******");
  
  }


  static final class Node
  {
  	private Node next;
  	private Integer data;
  	Node(Integer data)
  	{
  		this.data = data;
  	}
  }
}

- sin November 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SortedCircularList
{
  //sentinel node.
  private final Node head;
  
  public SortedCircularList()
  {
  	head = new Node(null);
  	head.next = head;
  }
  
  public static void main(String ...args)
  {
     SortedCircularList list = new SortedCircularList();
     list.add(new Node(4));
     list.add(new Node(10));
     list.add(new Node(14));
     list.print();

     
     //Adding a node in between.
     list.add(new Node(0));
     list.print();
     list.add(new Node(11));
     list.print();     
  }
  
  
  public void add(Node n)
  {
     if(head.next==head)
     {
     	head.next = n;
     	n.next = head;
     	return;
     }
     
     //find the right pos.
     Node prev = head.next;
     Node cur = head.next.next;
     while(cur!=head && cur.data<=n.data)
     {
     	prev = cur;
		cur = cur.next;
     }
     
     n.next = prev.next;
     prev.next = n;
     if(n.data<prev.data)
     {
        int temp = n.data;
        n.data = prev.data;
        prev.data = temp;
     }     
  }
  
  
  
  
  
  public void print()
  {
     Node n = head.next;
     while(n!=head)
     {
     	System.out.println(n.data);
     	n = n.next;
     }
     System.out.println("******");
  
  }


  static final class Node
  {
  	private Node next;
  	private Integer data;
  	Node(Integer data)
  	{
  		this.data = data;
  	}
  }
}

- sin November 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SortedCircularList
{
  //sentinel node.
  private final Node head;
  
  public SortedCircularList()
  {
  	head = new Node(null);
  	head.next = head;
  }
  
  public static void main(String ...args)
  {
     SortedCircularList list = new SortedCircularList();
     list.add(new Node(4));
     list.add(new Node(10));
     list.add(new Node(14));
     list.print();

     
     //Adding a node in between.
     list.add(new Node(0));
     list.print();
     list.add(new Node(11));
     list.print();     
  }
  
  
  public void add(Node n)
  {
     if(head.next==head)
     {
     	head.next = n;
     	n.next = head;
     	return;
     }
     
     //find the right pos.
     Node prev = head.next;
     Node cur = head.next.next;
     while(cur!=head && cur.data<=n.data)
     {
     	prev = cur;
		cur = cur.next;
     }
     
     n.next = prev.next;
     prev.next = n;
     if(n.data<prev.data)
     {
        int temp = n.data;
        n.data = prev.data;
        prev.data = temp;
     }     
  }
  
  
  
  
  
  public void print()
  {
     Node n = head.next;
     while(n!=head)
     {
     	System.out.println(n.data);
     	n = n.next;
     }
     System.out.println("******");
  
  }


  static final class Node
  {
  	private Node next;
  	private Integer data;
  	Node(Integer data)
  	{
  		this.data = data;
  	}
  }

}

- labscst November 08, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class InsertIntoSortedCircularLL {
public static void main(String args[]){
Node head = Node.createLinkedList();
Node curr =head;
while(curr.next != null)
curr=curr.next;
curr.next=head;
Scanner scan = new Scanner(System.in);
System.out.print("Enter element to insert : ");
int ele = scan.nextInt();
Node n = new Node(ele);
if(ele<=head.data){
n.next=head;
curr.next=n;
}
else{
curr=head;
while(curr.next.data<ele && curr.next!=head)
curr=curr.next;
n.next=curr.next;
curr.next=n;
}
curr=head.next;
System.out.print(head.data);
while(curr!=head){
System.out.print(curr.data);
curr=curr.next;
}
}

}

- Hermione December 05, 2016 | 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