
public boolean hasCycle(ListNode head) {
//Edge case
if(head == null || head.next == null)
return false;
ListNode slow = head, fast = head;
//Fast moves 2 steps and slow moves 1 step. If there is a cycle then both pointers will meet and that means the cycle exists in the list.
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(slow == fast)
return true;
}
//No cycle found so return false
return false;
}
O(n) time because all the nodes travel only once. But if there is a cycle then the nodes will be visited only for some constant time. And constant O(1) space because no extra memory is required to detect the loop. //The method returns the boolean result if there is a path.
public boolean solution(TreeNode root, int targetSum){
//Base case when we reach to the leaf node and reached the target sum
if(root.left == null && root.right == null){
if(targetSum-root.val == 0)
return true;
return false;
}
boolean left = false;
//Search on the left sub-tree for the path.
if(root.left != null){
left = solution(root.left, targetSum-root.val);
}
//Searching on the right sub-tree if there is no path in left sub-tree
if(!left){
if(root.right != null)
return solution(root.right, targetSum-root.val);
}
return left;
}
O(n). And we are using recursion, so the space complexity will be also O(n) in the worst case.class FreeTimeLearn {
List<String> ans; //answer that stores all the combinations.
//method that returns the possible letters from the given keys.
private String letters(char ch){
switch(ch){
case '2': return "abc";
case '3': return "def";
case '4': return "ghi";
case '5': return "jkl";
case '6': return "mno";
case '7': return "pqrs";
case '8': return "tuv";
case '9': return "wxyz";
default: return "";
}
}
//Method that recursively explores the path to generate the answer.
private void solution(String digits, int i, StringBuffer sb){
//Base case when letters end then the possible combination of letters are added to the answer list.
if(i == digits.length()){
ans.add(sb.toString());
return;
}
//Getting each character and generating all the combinations.
String letter = letters(digits.charAt(i));
for(int j = 0; j < letter.length(); j++){
sb.append(letter.charAt(j));
//Recursively generating the other possible combination of letters.
solution(digits, i+1, new StringBuffer(sb));
//Backtracking
sb.deleteCharAt(sb.length()-1);
}
}
public List<String> combinationLetters(String digits) {
ans = new ArrayList<>();
solution(digits, 0, new StringBuffer());
return ans;
}
} The first line indicates the integer 'T', which is the number of test cases. Each test case consists of:
The first line contains the elements of the first linked list, separated by spaces, ending with -1.
The second line contains the elements of the second linked list, separated by spaces, ending with -1.
For each test case, output the merged linked list, with elements separated by spaces, ending with -1. Each test case's output should be on a new line.
1 4 5 -1
2 3 5 -1
1 2 3 4 5 5 -1
1 <= T <= 10
1 <= L <= 10^5
1 ≤ data ≤ 10^6 and data != -1
//This Function returns the list of nodes that are visible from the right side.
//This function accepts the root node of the tree.
public List<Integer> rightSideView(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if(root == null)
return ans;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int n = queue.size();
for(int i = 0; i < n; i++){
TreeNode curr = queue.poll();
//adding the node to the answer if it is the last node .
if(i == n-1)
ans.add(curr.val);
//Add the child node to the queue.
if(curr.left != null)
queue.add(curr.left);
if(curr.right != null)
queue.add(curr.right);
}
}
return ans;
}
In the above function, the TreeNode structure will be like -
public class TreeNode {
int val;
TreeNode left; //Pointer to the left-subtree.
TreeNode right; //Pointer to the right-subtree.
}
public class FreeTimeLearning
{
public static int maxProfit(int[] prices) {
//Variable to keeptrack of minimum price
int minPrice = Integer.MAX_VALUE;
//Variable to keep track of maximum profit seen so far.
int profit = 0;
for(int i = 0; i < prices.length; i++){
minPrice = Math.min(minPrice, prices[i]);
profit = Math.max(profit, (prices[i] - minPrice));
}
return profit;
}
public static void main(String[] args) {
System.out.println(maxProfit(new int[]{4,7,1,8,2,9}));
}
}
OOP is a programming paradigm based on the concept of “objects,” which bundle data (attributes) and behavior (methods).
Class & Object
Encapsulation
Inheritance
Polymorphism
Abstraction
Languages like Java, Python, C++, C# use OOP.
Large-scale applications
GUI-based systems
Simulation software
Game development
OBP is similar to OOP but does not support inheritance. It deals with objects but lacks full OOP features.
No inheritance
Might not fully support polymorphism
Still uses objects to encapsulate data and functions
Languages like JavaScript (pre-ES6), VBScript, and early versions of Java are object-based.
It depends on the project and your needs:
| Criteria | OOP | OBP |
|---|---|---|
| Flexibility | High (due to inheritance, polymorphism) | Moderate |
| Complex Systems | Better suited | Not ideal |
| Performance | Can be heavier due to features | Often lighter |
| Ease of Learning | A bit more complex to grasp fully | Easier to start with |
You're building large, scalable, or maintainable systems.
You need reuse and extensibility (via inheritance and polymorphism).
Your project is small or medium-scale.
You want object encapsulation without the full complexity of OOP.