## Punit Jain

BAN USERYes, we will have to free it manually when we are done with it.

- Punit Jain January 06, 2013Its simple.. free (p) will do the job

- Punit Jain January 06, 2013Use Radix sort.

- Punit Jain May 16, 2012No.

- Punit Jain May 16, 2012@devesh

Do you always follow syntax? There is a thing called logic. Btw you can modify the function internally.

Could you please explain about this, probably logic ?

- Punit Jain May 15, 2012Push elements in stack in inorder.

Then pop each element and assign its successor. Complexity O(n);

Stack stack

Inorder (node *root)

{

if (root == NULL)

return;

Inorder (root->left);

stack.push (root);

Inorder (root->right);

}

node *p1=NULL, *p2=NULL;

while (!stack.empty) {

p2 = stack.pop;

p2->next = p1;

p1 = p2;

}

The subarray should be "15 1 11 -15 18" not "15 1 11".

Pseudo code

sum = 0, max = 0, start, end

a = 1,

for i = 1 to N

{

sum = sum + arr[i];

if (sum > max) {

max = sum;

end = i;

start = a;

}

if (sum < 0) {

a = i+1;

sum = 0;

}

}

The maximum sum subarray would be from arr[start] to arr[end]

Do BFS from start node and find the nodes which has distance K, print them.

The other solution:

Distance (Node *node, dist, k, Node *parent, Node *child) {

if (node == NULL)

return;

if (dist == k) {

print (node->data);

return;

}

// Passing parent and child to avoid the same node to be visited again.

if (node->left != child)

Distance (node->left, dist+1, k, node, NULL);

if (node->parent != parent)

Distance (node->parent, dist+1, k, NULL, node);

if (node->right != child)

Distance (node->right, dist+1, k, node, NULL);

}

main () {

Distance (Node *node, 0,k, NULL, NULL);

}

I was asked the same question. Write a recursive program to compute the number of paths from top left to bottom right.

I couldn't write that time but later I tried at home and did it.

Here is the pseudo code:

int Number_of_Paths (node *head)

{

if (!head)

return 0;

if (head->right && head->down)

return ( Number_of_Paths(head->right) + Number_of_Paths(head->down));

else if (head->right)

return Number_of_Paths (head->right);

else if (head->down)

return Number_of_Paths (head->down);

else

return 1;

}

This looks like a good solution. nCr could be written as nCn-r.

So (m+n-2)C (n-1) could also be written as (m+n-2)C(m-1).

So it could be vice versa

Approach would be right if string length is even. It wouldn't work for string which is of odd length.

- Punit Jain May 12, 2012what about 3x3 grid ? Answer should be 12 according to you but its 6.

- Punit Jain May 12, 2012elegant solution

- Punit Jain May 12, 2012Do ps -AL | grep pid

1st column is parent id and the second column is thread (LWP) id. if both are same then its a process id otherwise thread.

when dynamic allocated memory is not freed after its use. This result in the memory leak.

- Punit Jain May 12, 2012To implement integer array of size array_size

int *p = (int *) malloc (sizeof(int) * array_size);

now you can assign values using p[i] = something // i =0 to array_size-1

and and access them using same;

yeah.. looks like a good solution.

- Punit Jain May 08, 2012How ?

- Punit Jain May 07, 2012reverse the whole string

then reverse by words.

reverse (str);

while (*str != '\0') {

start = str;

while (*str != ' ' && *str != '\0')

{

str++;

}

end = str-1;

while (start < end)

{

char c; // to swap characters

c = *start;

*start = *end;

*end = c;

start++;

end--;

}

if (*str != '\0')

str++;

}

This one is classy problem :

classic-puzzles.blogspot.in/2006/12/google-interview-puzzle-2-egg-problem.html

pick any 6 balls divide them into 3 each.

if they are same check the rest two and you will find the heavier.

if they are not same then choose 3 from heavier bowl and pick any two and weigh them, if they are same third one is heavier otherwise its clear.

so you have to weigh only two times.

const char *p, char * const p

first is constant character pointer, the value pointed by p is constant.

second one is a constant pointer to character, where the value of p (address) is constant.

Yes if it is not a waited graph. It can be done by slightly modifying BFS algorithm.

for u , v E all edges connected to u. // just a reference from bfs algo

if v is already marked gray and check if d (v) == d(u) + 1, increment number of shortest paths to v by 1

otherwise if v is white set number of shortest path to 1, d(v) = d(u) +1.

You can also modify dijkstra's Algorithm but complexity would increase (in case of weighted graph).

Use BFS to find strongly connected components in undirected graph return the largest one.

O(|V| + |E|)

Using expected value of first 100 outcomes. black 90 white 10.

remains 810 black and 90 white. so probability would still be 9/10

there are two cases in which india win:

1) Akbar tells the truth and Amar tells the truth : 3/4*3/4 = 9/16

2) Akbar tells a lie that India loose and Amar tells lie to anthony that "Akbar told me india win" : 1/4*1/4 = 1/16

So total probability of winning India would be 9/16 + 1/16 = 10/16

Correct me if I am wrong.

The code is correct and produces right output.

What is the problem in using global variable ? or you are unfamiliar with them ?

This means that each node will contain its data + sum of all node's data in right subtree.

node->data = node->data + (sum of all nodes in right subtree).

Reverse Inorder would do the work for us.

Code:

int sum = 0; //global variable

sum_max (node *root)

{

if (root == NULL)

return;

sum_max (root->right);

sum = sum + root->data;

root->data = sum;

sum_max (root->left);

}

Algo:

for each character in string

if its a left part i.e. (,{,[ Push in stack

else if its a right part pop from stack and match whether is appropriate left part of parenthesis.

any case doesn't match return FALSE.

return TRUE.

Answer is C. (if its a 16 bit integer)

- Punit Jain May 02, 2012Negative numbers are stored as 2's compliment . Assuming 32 bit integer answer would be

fffffff0.

1. get to middle element of string.

2. take two pointers p1 and p2 point to middle element(if string is of odd length, or two different middle elements incase of even length).

3. while (p1 >= start, p2 <= end)

{

if (*p1 !=*p2)

return FALSE;

p1--;

p2++;

}

return true;

nice

- Punit Jain April 22, 2012warning : overflow. random output

- Punit Jain April 22, 2012Algo.

1. find the longest palindrome in string.

2. repeat the procedure for remaining two strings (left and right side of palindrome).

Assume it has all distinct characters.

total number of permutations : n!

Algo:

start from first character and traverse till end. find how many characters are ranked before this, suppose this is x, now we know there are x * (n-1)! permutations are ranked above this.

Repeat the procedure for every character in string.

int n = strlen(str);

int rank = 0;

for (i = 0; i < n-1; i++) {

int x=0;

for (j = i+1; j<n ; j++) {

if (str[i] > str[j])

x++;

}

rank = rank+ x*((n-i-1)!)

}

return rank;

Use randomize selection to find the 2nd largest element i.e. element at N-1 th position.

solution 2.

int max1, max2 = 0;

for i = 1 to N

if (A[i] > max2)

max2 = A[i]

if (max2 > max1)

swap (max1, max2)

return max2

Hashing would be best approach for O(n).

n = length of array.

Before inserting each element to hash table look for if found n-- otherwise insert to table.

return n

What about hashing ?

Before insertion check whether it is present before Inserting if no insert it with value 1 and if yes then increment the value with 1.

Is it to find the zeros in binary format?

If yes then:

count = 0

while (n>0) {

count = count + !(n&1)

n=n>>1 //Right shift by 1

}

return count

To find in decimal format the

count = 0

while (n>0) {

count = count + (n%10>0 ? 0 : 1)

n = n/10;

}

return count

Algorithm for problem

j=1

for i = 1 to N

if (A[i] > 0)

A[j] = A[i]

j++

for i = j to N

a[i] = 0

This takes O(n) without using any buffer

Rep**annadwilliams31**, Applications Developer at National InstrumentsI am Anthony, Human Resources specialist with a decade of successful experience in hiring and employee management.I am a ...

Rep**rrobertgregory**, Nursing aide at Road Runner Lawn ServicesI am working as a Nursing Assistant . My duties include helping patients to bathe and get dressed, repositioning wheelchairs etc ...

Rep**JanetCKeel**, Accountant at ABC TECH SUPPORTHey i am Janet i am from Us and i leave here from last 4 years .My job is truck ...

Rep**pepsyfarely**, Front-end Software Engineer at 247quickbookshelpI am Pepsy , a security supervisor at Edge Services for the last 4 years managing security teams and corporate environments ...

Rep**rizzafilher**, Java Developer at Achieve InternetSorter with 3+ years of experience , performs duties in a safe manner in compliance with all local, state, and federal ...

Rep**BruceSwigert**, Android Engineer at AlteraI am Bruce,I have worked with various branches, including finance and HR, which permits me to encourage a productive ...

Rep**GigiTaylor**, abc at 8x8Dedicated English Mandarin Chinese translator with years of experience working in professional and scientific communities.Voracious reader and participant in ...

Rep**LaylaLee**, abc at AMDCreative and dedicated photo editor with experience in photojournalism and marketing material development. I like to explore Best tantrik in ...

Rep**NaomiAdams**, abc at 8x8Passionate educator for nearly 5 years with a strong desire to help students recognize the connection between learning and experience ...

Rep**garlandkrebs**, Animator at ADPHighly focused and seasoned magazine editor with vast experience in all stages of weekly and monthly magazine production. My aim ...

RepHey, I am Ruth and I am working as an interviewer.I take too many Interviews For my Company and ...

Rep**SamiyahKate**, Java Experienced at Agilent TechnologiesI am Samiyah, having 3 years of experience in assessing, diagnosing, screening, and preventing language, speech, and swallowing disorders. I ...

Rep**ha1904536**, Android test engineer at ABC TECH SUPPORTHello, I am an Executive recruiter. My role is to fill executive, high-level positions at companies. I have been practicing ...

Rep**DonnaTyler**, HR Executive freshers at Bloomberg LPI am Donna , a travel counselor who advises clients on travel options and tour packages, makes bookings , prepares tickets and ...

Rep**MillaSoth**, Intern at ADPI am Milla , a dedicated Emergency Response Technician regarded for performing advanced medical procedures with a high degree of accuracy ...

Rep**RoseReynolds**, Analyst at 8x8Hi everyone I am Lilla from the UK.I am passionate about two things, gym and book lover.Last topic ...

Rep**lushililly**, Area Sales Manager at ASAPInfosystemsPvtLtdI am Graphic designer from Watertown. a professional within the graphic design and graphic arts industry assembles together images, typography ...

Rep**JoseElkins**, Animator at ASUI am working as Human Resources Associates, and my duties are for obtaining, recording, and interpreting human resources information within ...

Rep**jeansboylan698**, Associate at 247quickbookshelpJeanBoylan, and I am a Financial examiner and I love my work. Apart from this, Nowadays I am doing new ...

Rep**AadavThomas**, abc at A9I am a well skilled and innovative Industrial Designer who focuses on taking products, noting product ideas and making them ...

Rep**AidenKim**, business planner manager at GPUExpert business strategist with a sound understanding of organizational development and sales. Persuasive negotiator who uses integrity and professionalism in ...

Rep**dianamowery95**, Consultant at Delve NetworksMy Name is Diana Mowery. I am from St. Louis and received a Bachelor Degree and My Masters Degree from ...

Rep**sraceymiller**, Android Engineer at ASAPInfosystemsPvtLtdAs a wellness pioneer, Life Time is reshaping the way consumers approach their health by integrating where we move, work ...

Rep**NaomiAllen**, abc at HUIMHard-working, passionate coach who excels at teaching children between the ages of 8 and 16 about the fundamentals of football ...

Rep**vickgonder9**, Integration Software Engineer at Arista NetworksI am Antonio , a well-experienced Sales Representative who has been working in the Suadela Investment for almost 2 years. I ...

Rep**harveyoberion**, Analyst at AMDHi, I am Harvey, from the USA. I am working as a soil scientist. I study soil as a natural ...

Rep**remiflo4**, Dyeing at Fabric Dyeing ServiceMid-level hair stylist with five years of experience working with men’s and women’s hair as well as children ...

Rep**MiaMiller**, abc at A9Experienced software engineer with a passion for developing innovative programs that expedite the efficiency and effectiveness of organizational success. Well-versed ...

Rep**leliakelsch6389**, Associate at 247quickbookshelpHello, I am a News Anchor. And I have completed all my studies from Portland. And today I am a ...

Rep**haroldknopph**, Android test engineer at AMDI am a publicity writer from Atlanta USA . I create an intended public image for individuals, groups, or organizations. I ...

Rep**PrankHwa**, Animator at Accolite softwareI am a Reporter and responsible for delivering updates and analysis on current events with the main goal to keep ...

Rep**seinarondal**, Applications Developer at ABC TECH SUPPORTI am Seina, a hardworking individual with many years of customer service experience seeking a position with a company utilizing ...

Rep**corinnedonon**, Backend Developer at A9I am Corinne, a highly organised, professional and self-motivated Dietitian with a great passion for nutrition and people’s well-being ...

Rep**earleneefranks**, Accountant at 247quickbookshelpI am working as an Information clerk and I love my job. I also love to read new Articles related ...

Rep**JaninaGilden**, Java Experienced at BoeingI am Janina , a Registered Nurse with 3 years experience providing healthcare to a variety of patients in different institutions ...

Rep**wastonlare**, Java Developer at BroadsoftWaston , an executive in the finance and accounting area of the industry with expertise in teaching, training and managing my ...

Rep**TerryMorales**, Accountant at ADPI assist other social and human service providers in providing client services in a wide variety of fields, such as ...

Rep**aaronyocoma**, U.S. marshal at Pacific StereoWorking as U.S. marshal at Pacific Stereo numerous years . I encountered heaps of things . With this I likewise like ...

RepI am from london. I am 27 year old. I work in a balanced fortune as a personal care aide ...

Rep**EviePaul**, Member Technical Staff at Abs india pvt. ltd.I am a Studio camera operator from Florida USA.I love to relax n' chill. love when it's cloudy ...

Rep**neilsjohn4563**, Android Engineer at ABC TECH SUPPORTHello, I am a multimedia artist. I have 4 years experience in this.A multimedia Artist is someone who is ...

Rep**bungayasorya**, Accountant at BrowserStackAn archeologist is an expert on history who gains expertise through experience with historical documents and artifacts. The archaeological record ...

Rep**JamieWilliams**, Android Engineer at Email Customer ServiceMy name is Jamie.and i am a Translator It's been almost 2 years since I worked in this ...

Rep**GalateeLabrie**, Accountant at ABC TECH SUPPORTBonjour, je suis organisateur de congrès. toutes mes études terminées depuis la france. et je suis actuellement en train de ...

Rep**jaksanjak17**, Sports official at Avant Garde Appraisal GroupI am a Sports official in Avant Garde Appraisal Group . in a variety of sports and competition, responsible for enforcing ...

Rep**shushichiku**, Associate at ASUPaulKenda is a Farmworker working at Jackhammer Technologies . I am a specialist in my work here. I manage different things ...

Rep**StellaMoore**, Analyst at ADPI am professional and dependable with more than 4 years of experience working at a bakery. I mix ingredients together ...

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Sorry, it was right shift, I have changed this. Shalini can you please elaborate through code. Thanks!

- Punit Jain June 30, 2013