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
https://forums.phpfreaks.com/topic/134991-php/#findComment-704406
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
https://forums.phpfreaks.com/topic/134991-php/#findComment-708063
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
https://forums.phpfreaks.com/topic/134991-php/#findComment-708070
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.