Swap every two nodes in a linked list

Given a singly linked list, swap every two nodes e.g. 1->2->3->4->5->6 should become 2->1->4->3->6->5 Solution: This can be done using two pointers. Take a current and temp pointer. At each iteration swap temp and current node. Repeat the steps until null node. You can think of the recursive solution of the problem. Implementation:

Probability of picking 2 socks of same color

Problem: There are 6 pairs of black socks and 6 pairs of white socks.What is the probability to pick a pair of black or white socks when 2 socks are selected randomly in darkness. This question was asked in Aamaon. Solution: Ways to pick any 2 socks from 24 socks = 24C2 Ways to pick Read More →

Age of 3 children – Mathematical Puzzle

Problem: Two old friends, Jack and Bill, meet after a long time. Jack: Hey, how are you man? Bill: Not bad, got married and I have three kids now. Jack: That’s awesome. How old are they? Bill: The product of their ages is 72 and the sum of their ages is the same as your Read More →

Maximum difference between two elements

Given an array arr[] of integers, find out the difference between any two elements such that larger element appears after the smaller number in array. Examples: If array is [2, 3, 8, 6, 4, 8, 1] then returned value should be 6 (Diff between 8 and 2). If array is [ 7, 9, 5, 6, Read More →

Red and Blue Marbles Puzzle

Puzzle: You have two jars, 50 red marbles and 50 blue marbles. You need to place all the marbles into the jars such that when you blindly pick one marble out of one jar, you maximize the chances that it will be red. When picking, you’ll first randomly pick a jar, and then randomly pick Read More →

Find Majority Element in an array in O(n)

The majority element is the element that occurs more than half of the size of the array. Algorithm below loops through each element and maintains a count of a[maj_index], If next element is same then increments the count, if next element is not same then decrements the count, and if the count reaches 0 then Read More →

Keys in RDBMS

We know that database uses tables to organize information. To maintain data integrity (that is data should be correct and in well formed) we use concept of keys. There are five types of keys in database which is as follows – Candidate key Primary Key Foreign Key Alternate Key Composite Key Example: STUDENT {SID, FNAME, Read More →

Mysql Query – Set 5

We have 3 tables Movie, Reviewer, Rating as shown below: Movie ( mID, title, year, director ) There is a movie with ID number mID, a title, a release year, and a director. Reviewer ( rID, name ) The reviewer with ID number rID has a certain name. Rating ( rID, mID, stars, ratingDate ) Read More →

Find largest BST in a binary tree

Given a Binary tree, Find largest Binary Search tree in it. A tree is a BST if: 1. Both left and right subtrees of a node are BSTs. 2. The node’s value is greater than its left subtree’s maximum value. 3. The node’s value is less than its right subtree’s minimum value. Top-Down approach : Read More →

D. E. Shaw Interview – Set 1

Location – Bits Pilani Campus Criteria :Cs/IT/ECE And CGPA min of 7.0[CSE] .For ECE =7.5 D. E. Shaw generally prefers computer science branch students but they’re not as strict as Amazon or Google and for for CS students it was only 7.0 . Luckily, my CGPA was above the cutoff and I was allowed to Read More →

Heap Data Structure

heapArray

Heap data structure is an array object that can be viewed as a nearly complete binary tree. Each node of the tree corresponds to an element of the array. The tree is completely filled on all levels except possibly the lowest , which is filled from the left up to a point. There are two Read More →

Abstract class

Before abstraction let’s first understand the abstraction. Abstraction Abstraction is the process of hiding the implementation details and showing only functionality to the user. It hides the internal details. Ways to achieve abstraction: 1. Interface 2. Abstract class Abstract class A class that is declared as abstract is known as an abstract class. It needs Read More →