Search Media Interview Question
Country: United States
Compile time:
#include <iostream>
#include <ostream>
template <int N>
struct printN
{
static void print(std::ostream& os)
{
printN<N-1>::print(os);
os << N << std::endl;
}
};
template <>
struct printN<1>
{
static void print(std::ostream &os)
{
os << 1 << std::endl;
}
};
int main() {
printN<100>::print(std::cout);
return 0;
}
#include<stdio.h>
void main()
{
for(int i=1;i<=100;i++)
printf("%d",i);
}
less no of lines
#include<conio.h>
struct
{
unsigned i:7;
}a;
void main()
{
clrscr();
while(++a.i!=100 && printf("%d",a.i));
getch();
}
EXPLANATION
look here-
1) BIT FIELD CONCEPT
i prefer bit field concept because we need only print 1-100 and it need only 7 bit so it save memory(space)
2)rather than using post increment i prefer pre increment becausethe implementation of post-increment (a++) must create a temporary storage for housing the original value of a, increment/store a, and return the original temporary value. In contrast, pre-increment (++a) only has to increment/store the value and return it – no temporary required.
IT ALSO MINIMIZE SAPCE (as compare to post increment)
3)the time complexity is O(n) and space complexity is just O(1) because here i used only one variable
4) but i am little bit confuse at comparison portion
in my opinion (!=) faster than < or > operator so thts the reson why i use !=.
What kind of stupid quesiton is this???? Complexities for a FIXED problem?
What's wrong with a loop? A few variables?
Here is the best I can come up with (in C like you want):
puts("1 2 3 4 5 6 7 8 9 10 11 12 <type it in here> 99 100");
//printf("1 2 3 4 .. 9 100") is more expensive because it is a varargs function
Can we use system clock to get start time, read till its start time + 100 units. Here we are neither creating any variable(space complexity), nor incrementing them (time complexity).
Depending on the programming language, the cost of reading the system clock is only task we will be performing.
Reading the system clock is usually going to be considerably more expensive than doing arithmetic or converting integers to strings. Besides, if you have to wait for the clock to change, you are probably constantly polling the clock, which means you're probably doing a lot more than just 100 reads. Comparisons to determine whether the time has changed since the last time you polled the clock are also not free.
It's not true that you won't create any variables, because you will need one to hold the time returned from the system clock, and you will also need a variable to hold the previous time returned by the system clock so that you can see whether it has changed. You'll also need one to hold the clock start time and / or the end time. You can see this program will have at least a few variables.
#include<stdio.h>
- vinil December 22, 2013void main()
{
for(int i=1;i<=100;i++)
printf("%d",i);
}
less no of line it achevies all properties of algorthims