Interviewstreet coding challenge input constraints [closed] - php

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
A question about interview street input constraints. (http://interviewstreet.com/)
Is it necessary to check the inputs for errors in the interviewstreet challenges?
For example, one challenge details the following constraints for the STDIN content:
1 <= N <= 1,00,000(10^5)
1 <= K <= N
0 <= profit value of any billboard <= 2,000,000,000(2*10^9)
Do I have to write some code to check the values to make sure that they meet these constraints or can I just assume that they do.
Also, if I do have to write the code to check what do I output if the inputs are incorrect?
Thanks

You can take it for graneted that inputs will always be adhering to the given constraints.
You don't need write any extra code for checking if input is within given constraints.
So if they say N will be <= 1,00,000, you can use an array of exactly 1,00,000 to store the elements and you'll be fine.

Related

SUM OF NUMBERS IN WHILE LOOP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I'm trying to find the sum of numbers by querying a database using PHP and MYSQL. I need a while loop or any other solution that can add numbers that are >= 40 from a sequence of numbers. I'm only able to make the query but don't know how to go about the rest.
$selectMARK="SELECT mark FROM resultstbl WHERE mark >= 40";
$queryMARK=mysqli_query($dbCon, $select);
$sum=0;
while($rowz=mysqli_fetch_assoc($queryMARK))
{
// need some code to add the numbers here
}
You maybe need an
$iterator = 0;
outside the loop, that you can increment inside of it.
$sum += $rowz[$iterator];
$iterator++:
Then do your condition around it by checking $rowz[$iterator] <= 40

PHP mysql query taking too long for using like to timestamps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX on the field and change your query to LIKE '2016-03%' (there won't ever be anything before the year in a timestamp so drop that first %). It'll be able to take a couple shortcuts, at least.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'
Try adding an INDEX to your time column and as other have pointed out, remove the leading % from the query.
See this post for more info

How to check if number of rows = 6n-1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I wish to count a number of rows in my sql database, and if the number of rows equal 6n-1 perform a function, if not, do nothing.
I know I can specify the integers to look for, but there would be thousands, so I wish to find a way where I can just define the math 6n-1
for example, when I count the rows in a table, I want to check whether there is 5,11,17 and so on.
If the number of rows match, do something, else do nothing.
Since 5,11,17.... is equal to every 6th row starting from 5, this can be expressed as 6n-1.
IS there a way to do this without defining every integer to check?
($rowcount + 1) % 6 == 0
perhaps...
for ( $i=1 ; 6*$i-1 <= $database_rows ; $i++ ) {
if ( 6*$i-1 == $database_rows ) {
do_something();
}
}

sorting alphanumeric in mysql? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
040, 044P, 041BL, 041W, 041PB
^^ This is the order it is coming out in by using Order By clause.
I think this is how it should appear instead:
040, 041BL, 041PB, 041W, 044P
I know similar questions must have been asked before, but I still can't figure out anything!
Edit: After X.L.Ant's comment, I realized my mistake. Therefore, simple order by clause is working for the test case given above. However, the case is still complicated if the number of digits is not always going to be 3 as GolezTrol mentioned. What should one do in that case?
Try:
SELECT string,
#num := CONVERT(string, signed) AS num_part,
Substring(Trim(LEADING '0' FROM string), Length(#num) + 1) AS rest_of_string
FROM table1
ORDER BY num_part,
rest_of_string
This way, the numbers will still be ordered by their numerical value (the leading 0s not being taken into account).
See fiddle.
try LPAD() function - but remeber that LPAD trims digits.
http://sqlfiddle.com/#!2/d7281/3/0

check array for limitations [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am accepting form submissions.
To prevent spoofing of the form I am using php isnumeric to verify that the posted values contain only numeric values.
I would like to check also:
1) that the posted values array contains max 1000 values (because no user would buy more than 1000 items!)
2) that the size of a single array key is composed of max 20 numbers (bigint unsigned max length)
how do I achieve this?
Point 1:
if (count($posted_values) <= 1000)
{
...
}
else
echo "Error";
Point 2:
Did you mean PHP_INT_MAX ?
If yes, just do:
foreach($posted_values as $value)
and then check that $value is less or equal than PHP_INT_MAX.

Categories