Primary Key, Foreign Key, and Index
Problem
Question: Explain the differences between a Primary Key, a Foreign Key, and an Index in SQL.
Answer:
Technical
1 sections, 6 questions
Question: Explain the differences between a Primary Key, a Foreign Key, and an Index in SQL.
Answer:
Question: What is the difference between == and === in JavaScript?
Answer:
Question: Write code to merge two sorted linked lists into one sorted linked list.
Solution (Java):
public class MergeSortedLinkedLists {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
public static Node mergeTwoLists(Node l1, Node l2) {
Node dummy = new Node(0);
Node current = dummy;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
}Question: You have 3 bags — one containing only red balls, one with only blue balls, and one with a mix of red and blue balls. All bags are labeled incorrectly. You may pick balls from only one bag to correctly re-label all three bags. Which bag would you open?
Answer: Open the bag labeled "Mixed". Since all labels are wrong, this bag cannot actually be mixed — it must be either all red or all blue. Draw one ball:
Question: What is a View in DBMS?
Answer: A View is a virtual table based on the result of a SQL query. It does not store data physically (unless materialized) but presents data from one or more underlying tables. Views are used to simplify complex queries, restrict access to specific columns/rows, and provide a consistent interface even if the underlying schema changes.
Question: Write code for the in-order traversal of a binary tree.
Solution (Java):
public class BinaryTree {
static class Node {
int val;
Node left, right;
Node(int val) { this.val = val; }
}
public static void inorder(Node root) {
if (root == null) return;
inorder(root.left);
System.out.print(root.val + " ");
inorder(root.right);
}
}