Jump to content

PHP


shamuntoha

Recommended Posts

php interview asked few questions. i failed to answer it in 1 minute. does anyone know it?

 

Q.1 what is the x and y value ?

x+y = 3

x-y = 4

 

Q.2 what is the result?

{1, 2, 3, 4, 6}

 

 

I've always heard Q1 called a "system of equations."  Like others have said, that one is quite easy.  Ones with 3+ variables and exponents (above 1, of course) are a bitch (not hard, just time consuming).

 

 

The basic concept is this:

 

a + b = c

b - c = e

 

If you can find the value of one of the variables in terms of the others, you can get it to something solvable:

 

b = -a + c

So: b - c = e is equiv to -a + c - c = e, so -a = b.

 

(You would of course need a constant in there before you could solve for real numbers.)

 

 

So, to solve your question 1, it would be:

 

x+y = 3

x-y = 4

 

x = -y + 3

 

-y + 3 - y = 4

-2y = 1

y = -1/2

 

x = 1/2 + 3

x = 3.5

 

 

Then, to check 3.5 + (-.5) = 3, 3.5 - (-1/2) = 4

Link to comment
Share on other sites

Or to shorten that into words:

 

Solve for either X or Y in one equation. Substitute that value in the first equation, and solve for the other one. Take the answer from that and substitute it into the original equation and solve for the first variable.

Link to comment
Share on other sites

Similar to corbin, I solved #1 by solving for 1 of the variables, except I did it by adding the equations (since the y's cancel eachother out)

 

x + y = 3

x - y = 4

 

---------

 

2x = 7

 

---------

 

x = 3.5

 

then plug the 3.5 in for x and voila.

 

It shaved a little time off for me (and/or a little paper if you were writing it out) but it would only work if one of the variables cancelled out.

Link to comment
Share on other sites

I've actually created main matrix for this set of equations

    | 1  1 |
W = | 1 -1 |

And also Cramer's matrices for each variable

     | 3  1 |
Wx = | 4 -1 |

     | 1  3 |
Wy = | 1  4 |

 

Then I calculated determinants for each of them

det(W) = -2 != 0

det(Wx) = -7

det(Wy) = 1

 

And calculated both variables using Cramer's rule

 

x = det(Wx)/det(W) = 3.5

y = det(Wy)/det(W) = -0.5

 

Now. Who solved this using graphical method?  :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.