wu :: forums
« wu :: forums - Bool Function »

Welcome, Guest. Please Login or Register.
Dec 2nd, 2024, 7:41pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Icarus, towr, SMQ, william wu, Eigenray, Grimbal, ThudnBlunder)
   Bool Function
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Bool Function  (Read 630 times)
sulfur
Newbie
*





   


Posts: 18
Bool Function  
« on: Feb 7th, 2010, 8:01pm »
Quote Quote Modify Modify

Construct a function which returns TRUE or FALSE.
It returns TRUE if the function is invoked less than n times in last m minutes else returns FALSE.
The expected answer is to be done in constant space.
 
e.g. n=5 times ; m=2 minutes
 
ith Minute       No. of times Func called        
                  
         1                     3  ( Returns 3 TRUE's )
         2                     2  ( Returns 2 TRUE's )
         3                     4  ( Returns 3 TRUE's & 1 FALSE )  
« Last Edit: Feb 7th, 2010, 8:03pm by sulfur » IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Bool Function  
« Reply #1 on: Feb 8th, 2010, 1:20am »
Quote Quote Modify Modify

I am afraid you will need O(m) space.
« Last Edit: Feb 8th, 2010, 1:20am by Grimbal » IP Logged
Hippo
Uberpuzzler
*****





   


Gender: male
Posts: 919
Re: Bool Function  
« Reply #2 on: Feb 8th, 2010, 3:28am »
Quote Quote Modify Modify

on Feb 8th, 2010, 1:20am, Grimbal wrote:
I am afraid you will need O(m) space.

O(n), not O(m) I think. Or may be in O(min(m,n)).
« Last Edit: Feb 8th, 2010, 3:30am by Hippo » IP Logged
sulfur
Newbie
*





   


Posts: 18
Re: Bool Function  
« Reply #3 on: Feb 8th, 2010, 7:02am »
Quote Quote Modify Modify

Quote:
I am afraid you will need O(m) space.

 
 
Yeah. You are absolutely right. The implementation code needs O(m) space. I used Queue for that . But the interviewer was interested in looking a solution which involves constant space. Embarassed
« Last Edit: Feb 8th, 2010, 7:03am by sulfur » IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Bool Function  
« Reply #4 on: Feb 8th, 2010, 7:44am »
Quote Quote Modify Modify

on Feb 8th, 2010, 3:28am, Hippo wrote:

O(n), not O(m) I think. Or may be in O(min(m,n)).

I agree with O(min(m,n)).
 
Either store the time of the last n calls, or store the counters for the last m minutes.
IP Logged
JohanC
Senior Riddler
****





   


Posts: 460
Re: Bool Function  
« Reply #5 on: Feb 8th, 2010, 8:30am »
Quote Quote Modify Modify

on Feb 8th, 2010, 7:02am, sulfur wrote:
Yeah. You are absolutely right. The implementation code needs O(m) space. I used Queue for that . But the interviewer was interested in looking a solution which involves constant space. Embarassed

Maybe she/he expected you to tell that an exact solution seems impossible, but that good aproximations could be practical?
So, rephrasing, what would be the best approximation using constant space?
IP Logged
bmudiam
Junior Member
**





   


Gender: male
Posts: 55
Re: Bool Function  
« Reply #6 on: Feb 8th, 2010, 6:48pm »
Quote Quote Modify Modify

I will maintain two variables.
 
var1 = number of minutes passed
var2 = number of calls made.
 
Now my function will look at follows.
<code>
if( var1 > m || var2 > n)
return false
else  
return true
</code>
 
I think this is constant time as you are storing only two variables.
IP Logged

“Nobody can go back and start a new beginning, but anyone can start today and make a new ending.” - Maria Robinson
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Bool Function  
« Reply #7 on: Feb 9th, 2010, 5:48am »
Quote Quote Modify Modify

And when and how do you update var1 and var2?
IP Logged
bmudiam
Junior Member
**





   


Gender: male
Posts: 55
Re: Bool Function  
« Reply #8 on: Feb 9th, 2010, 5:50am »
Quote Quote Modify Modify

after every minute and every function call.
IP Logged

“Nobody can go back and start a new beginning, but anyone can start today and make a new ending.” - Maria Robinson
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Bool Function  
« Reply #9 on: Feb 9th, 2010, 6:09am »
Quote Quote Modify Modify

You increment var1 every minute?  That would mean after m minutes you always return false.  In the example above, you would always return FALSE in the 3rd minute, and not 3 time TRUE and 1 time FALSE.
IP Logged
Paragon
Newbie
*





   


Posts: 2
Re: Bool Function  
« Reply #10 on: Feb 9th, 2010, 8:17am »
Quote Quote Modify Modify

Just to be precise shouldn't it be O(min(m,n-1))?
hidden:
You can store the latest call on stack. And after we are done with the comparisons replace it with the oldest.
IP Logged
Aryabhatta
Uberpuzzler
*****






   


Gender: male
Posts: 1321
Re: Bool Function  
« Reply #11 on: Feb 9th, 2010, 10:50am »
Quote Quote Modify Modify

on Feb 9th, 2010, 8:17am, Paragon wrote:
Just to be precise shouldn't it be O(min(m,n-1))?
hidden:
You can store the latest call on stack. And after we are done with the comparisons replace it with the oldest.

 
Well if you really want to be precise, the space usage is Omega(min(m,n)).
IP Logged
Hippo
Uberpuzzler
*****





   


Gender: male
Posts: 919
Re: Bool Function  
« Reply #12 on: Feb 9th, 2010, 3:29pm »
Quote Quote Modify Modify

Or (min(m,n)). Smiley, Yes I have known the O was not accurate, but I didn't want to confuse by correcting 2 things in on reply. Later I have discovered both m and n solutions exist ...
« Last Edit: Feb 9th, 2010, 3:29pm by Hippo » IP Logged
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Bool Function  
« Reply #13 on: Feb 9th, 2010, 5:05pm »
Quote Quote Modify Modify

O(min(m,n)), O(min(m,n-1)) and O(min(2m+1000,3n+1000)*1000) are all the same.
« Last Edit: Feb 9th, 2010, 5:07pm by Grimbal » IP Logged
Aryabhatta
Uberpuzzler
*****






   


Gender: male
Posts: 1321
Re: Bool Function  
« Reply #14 on: Feb 9th, 2010, 9:29pm »
Quote Quote Modify Modify

on Feb 9th, 2010, 3:29pm, Hippo wrote:
Or (min(m,n)). Smiley, Yes I have known the O was not accurate, but I didn't want to confuse by correcting 2 things in on reply. Later I have discovered both m and n solutions exist ...

 
True Smiley
IP Logged
srikanth.iiita
Newbie
*





   


Posts: 33
Re: Bool Function  
« Reply #15 on: Feb 9th, 2010, 10:11pm »
Quote Quote Modify Modify

For each test case.. we are required to take at least
m (minutes information & number of calls in each minute).. so the input should be O(m) .  
 Can't we use the same storage of O(m) for solving.Huh
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board