Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Check if an array has duplicate numbers in O(n) time and O(1) space

We have array  a[ ]= {1, 2, 3, 4, 5, 6, 7, 8, 4, 10};
And we need to find a number which is duplicate in O(n) and Space complexity O(1)

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Given LinkedList and K=2, N=4

1->2->3->4->5->6->7->8->9

so Partitioned LinkedList will be

1->2->3->4->5->6->7->8->9

Now delete first K nodes from each subpart and return linked list

3->4->7->8

Here first 2 nodes are deleted from each subparts.

Find two non repeating elements in an array of repeating elements

Given an array in which all numbers except two are repeated once.
(i.e. we have 2n+2 numbers and n numbers are occurring twice and remaining two have occurred once).
Find those two numbers in the most efficient way.
example :
Arr = [1,2,3,1] => 2, 3 occurring once
Arr = [2, 3, 7, 9, 11, 2, 3, 11] => 7, 9 occurring once

Given array of 0’s and 1’s. All 0’s are coming first followed by 1’s. find the position of first 1

Example : 00001111
Output : 4

Method 1 : in O(n) we can do it . with sequential search.

Method 2 : Binary search.
Yes binary search is useful, as data is sorted.
Just we need to change its conditions for 0 and 1.
lets have a look at Function firstOccurance.

Max Sum in circularly situated Values

This question was asked first in FACEBOOK On Campus for Internship.
There are N trees in a circle. Each tree has a fruit value associated with it.  
A bird can sit on a tree for 0.5 sec and then bird have to move to a neighboring tree. It takes the bird 0.5 seconds to move from one tree to another. The bird gets the fruit value when she sits on a tree. 
We are given N and M (the number of seconds the bird has), and the fruit values of the trees. We have to maximize the total fruit value that the bird can gather. The bird can start from any tree.

Find if two rectangles overlap

Given two rectangles, find if the given two rectangles overlap or not.












Note that a rectangle can be represented by two coordinates, top left and bottom right. So mainly we are given following four coordinates.

Implement LRU Cache

Generally Interviewer can ask you simply to implement LRU cache, first explain which data structure you will use and why.

Its well known coding question being asked by all Tech giants like Amazon , Flipkart, Microsoft. 

LRU (Least Recently Used) Cache holds Page numbers which are recently used and throws out old or least recently used page number or entries.

Given Set of words or A String find whether chain is possible from these words or not

You are given N strings or N words. You have to find whether a chain can be formed with all the strings given namely n ?
Chain : A chain can be formed between 2 strings if last character of the 1st string matches with the first character of the second string.
Example : cat ,eng, galactic, tree
These words Create chain as  ...  TREE,ENG,GALACTIC,CAT or CAT, TREE, ENG, GALACTIC

Try to think as its simple solution.

Memory Efficient LinkedList

Memory efficient linked list is also called XOR Linked List.
To use lesser memory in doubly link list, such data structure is used.

An ordinary doubly linked list stores addresses of the previous and next list items in each list node, requiring two address fields:

...  A       B         C         D         E  ...
          –>  next –>  next  –>  next  –>
          <–  prev <–  prev  <–  prev  <–

An XOR linked list compresses the same information into one address field by storing the bitwise XOR (here denoted by ⊕) of the address for previous and the address for next in one field:

...  A        B         C         D         E  ...
         <–>  A⊕C  <->  B⊕D  <->  C⊕E  <->

Sort an array according to the order defined by another array

Given two arrays A1[] and A2[], sort A1 in such a way that the relative order among the elements will be same as those are in A2. For the elements not present in A2, append them at last in sorted order.
Input: A1[] = {2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8}
       A2[] = {2, 1, 8, 3}
Output: A1[] = {2, 2, 1, 1, 8, 8, 3, 5, 6, 7, 9}
The code should handle all cases like number of elements in A2[] may be more or less compared to A1[]. A2[] may have some elements which may not be there in A1[] and vice versa is also possible.
Given Code in: O(m*count + n) Space complexity O(m)
where m=size of A1,n=size of A2 count= maximum 
occurrence of any element of A1.

Company Asked : Amazon

Get Minimum element in O(1) from input numbers

Question : Get Minimum element in O(1) from input numbers..

- With any traditional way we can't get minimum element in O(1)
- so we need to come up with different data structure.
- As its very old and famous question We are directly explaining logic

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Given an array of n distinct integers sorted in ascending order, write a function that returns a Fixed Point in the array such that Arr[i] = i .
If there is any Fixed Point present in array, else returns -1.
Note that integers in array can be negative.

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Main DNA sequence(a string) is given (let say strDNA) and another string to search for(let say strPAT).
You have to find the minimum length window in strDNA where strPAT is subsequence.

Add Sub Multiply very large number stored as string

(Flipkart Machine coding round)
Very large number like 2994320121 and 125346232 is given in character string.
You need to add, subtract and multiply two numbers and store number in character array only.

Code Chef PRGIFT Aug 14

Today is chef's friend's birthday. He wants to give a gift to his friend. So he was desperately searching for some gift here and there.
Fortunately, he found an array a of size n lying around. The array contains positive integers. Chef's friend likes even numbers very much. So for the gift, chef will choose a consecutive non-empty segment of the array. The segment should contain exactly k even integers. Though it can have any number of odd integers.

Find and print longest consecutive number sequence in a given sequence in O(n)

Ex: Input: 1 2 5 3 6 8 7
    Output: 5 6 7 8 
As given in Above example we need to find Longest Consecutive sequence in O(n)
I have implemented in O(n) considering by creating hash  of data type bool.

Find min element in Sorted Rotated Array (With Duplicates)

Problem: Find the minimum element in a sorted and rotated array if duplicates are allowed:
Example: { 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 1, 2}   output: 0
Algorithm:

Find min element in Sorted Rotated Array (Without Duplicates)

Problem: Find the minimum element in a sorted and rotated array if duplicates not allowed:
Example: {3, 4, 5, 6, 7, 1, 2} output: 1

CodeChef Code SGARDEN

This time Sheriff gathered all the bandits in his garden and ordered them to line up. After the whistle all bandits should change the order in which they stand.
Sheriff gave all the bandits numbers from 1 to N. For each place i he determined the unique position j. After whistling the bandit staying on position i should run to the j-th position. Sheriff loved seeing how the bandits move around, and he continued whistling until the evening. He finished the game only when he noticed that the bandits are in the same order in which they were standing originally.
Now the Sheriff asks the question: How many times has he whistled?

Check Binary Tree is Binary Search Tree or not

To search Binary Search Tree
First lets understand characteristics of BST
• The left subtree of a node contains only nodes with keys less than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• Both the left and right subtrees must also be binary search trees.
• Each node (item in the tree) has a distinct key.