Wandering programmer
BAN USER- 0of 0 votes
AnswerGiven a point and a radius r, draw a circle.
- Wandering programmer in United States| Report Duplicate | Flag | PURGE
Pure Storage Software Engineer C C++
bool isSquare(Point p1, Point p2, Point p3, Point p4)
{
int d2 = distSq(p1, p2); // from p1 to p2
int d3 = distSq(p1, p3); // from p1 to p3
int d4 = distSq(p1, p4); // from p1 to p4
// If lengths if (p1, p2) and (p1, p3) are same, then
// following conditions must met to form a square.
// 1) Square of length of (p1, p4) is same as twice
// the square of (p1, p2)
// 2) Square of length of (p2, p3) is same as twice the square of (p1, p2)
if (d2 == d3 && 2 * d2 == d4 && 2 * d2 == distSq(p2, p3)) {
int d = distSq(p2, p4);
return (d == distSq(p3, p4) && d == d2);
}
// The below two cases are similar to above case
if (d3 == d4 && 2 * d3 == d2 && 2 * d3 == distSq(p3, p4)) {
int d = distSq(p2, p3);
return (d == distSq(p2, p4) && d == d3);
}
if (d2 == d4 && 2 * d2 == d3 && 2 * d2 == distSq(p2, p4)) {
int d = distSq(p2, p3);
return (d == distSq(p3, p4) && d == d2);
}
return false;
}
This will not compile because the functions foo() and bar() and printf() are not defined in main.c
In order for this to work an additional header is needed:
// additionalheader.h
void foo();
void bar();
The following lines must be added to the routine main():
#include <stdio.h>
#include "additionalheader.h"
The variable var will not be redefined because it is static and therefore will not be visible outside the file in which it is defined.
The result of running the resulting executable will be the value assigned to 'var' in main(). In this case that will be '0' because this is required by the C language standard.
gevorgk is correct. Move to the t'th element in the list and delete that element and all remaining elements in the list.
- Wandering programmer May 21, 2010>
>> I really hate it when people ask such questions, I am wondering,
>> they just want to pull of peoples tolerance levels towards such
>> questions, Instead they could beat him with a stick and watch if
>> get angry:)
>
No, this isn't about measuring someone's tolerance level, it's about measuring an individual's understanding of the C language. Few, if any, programmers, would knowingly use malloc() to allocate 0 bytes of memory. However, it would be very easy to pass an argument to malloc() that somehow got incorrectly set to zero. The interviewer wants to know if you understand what will happen if such a situation occurs. This is going to be important when you attempt to find the bug that has set the variable in question to a value of zero.
The correct answer to this question is that the result is compiler dependent. However, the most likely behavior is that a non-zero length memory segment will be returned by malloc(). Malloc is guaranteed to return *at least* the number of bytes you request, but may return a contiguous memory segment larger than the requested size if it wishes. Since some malloc() implementations maintain "bins" of preallocated memory it's possible that the size of the memory segment returned will be small, but not zero. Perhaps 8, 16, or 32 bytes in length.
All of these solutions are of order n where n is the bit to be cleared. This can, however, be done in constant time with a lookup table. Build a table (an array) that has a preset bitmask for each bit in the integer. The 'Clear' function then looks up the mask using the value of n passed in. The mask is then applied and the modified value of x returned.
O(n) is too much for a function like this that may be used many times. The lookup table solution is O(1).
If the following three statements in three statements:
int i=100;
extern int i=100;
static int i=100;
are placed into three separate files that are fed to a compiler to be compiled into a single executable, the result will be an error concerning multiple definitions of the symbol i.
The extern statement is used to give a "heads-up' to the compiler that the symbol in question is defined "externally" to the current compilation unit, in this case the file. If the variable is set to 100 in the extern statement, then there will be two definitions of the variable. One may choose to the first statement to be:
int i;
in which case the three files can compile into a single executable. If this is done, however, the compiler will give a warning that a declaration occurs in an 'extern" statement. This is appropriate since the extern statement should not reserve storate.
It is illegal to point to an object of the base class with a pointer that is of the derived class type.
Base *b = new Derived(); // compiles properly
It seems to me that the phrase "a dictionary is given" is an ambiguous phrase. I would ask the interviewer what this phrase meant.
If it meant that a data structure representing the dictionary was provided, I would perform what ever lookup was appropriate for the data structure. I cannot imagine a complexity greater for such a lookup that would be greater than N and any attempt to fill an additional data structure would be at best N before the lookup could be performed.
If it meant that a file of words was provided, it gets pretty tricky. Are the number of entries in the file known? Is the format of the file known? Can the position of entry n/2 be easily calculated? If so, I would perform a binary search on the disk file. I am aware that this would require a lot of disk accesses, but I doubt that it would be expensive than reading the while disk file into some more accessible data structure.
Only if the format of the disk file made a binary search difficult would I read the data into a data structure.
Interesting question. If a turn is one roll for you and one roll for me, 1 in 6. However, if we say that the game continues until someone rolls a winning number, my chances go up to 1 chance in 2 since someone will win.
- Wandering programmer March 01, 2010A lookup table for the 255 bit patterns in a byte provides constant time performance.
- Wandering programmer March 01, 2010So a bit of basic math.
4 bytes counts 4294967296 structures.
1 sq mi = 27878400 sq ft.
My apartment is about 1200 ft sq. If all the buildings in NY had a footprint on average that was half the size of my apartment (pretty small for lots of buildings in NY. The area that could be counted by the counter would be
4294967296 * 600 ft sq = 2.5769803776 e+12 ft sq
which is
92436 mi sq
or an area 304 mi by 304 miles. If the average building footprint is larger than my apartment, then the size of the area grows.
Yup... I think the counter is big enough at 4 bytes.
No... the assignment operator will be called to assign a to c. In this context the value of 2 contained in c.x will be overwritten by the value of 4 contained in a.x. This new value will then be decremented to 3. I suggest you compile and run the following example
#include <iostream>
using namespace std;
// Online Test
class Someclass {
public:
int x;
public :
Someclass(int xx) : x(xx) { }
Someclass(const Someclass& a) { x = a.x ; x++;}
Someclass& operator =(const Someclass& a1) { x = a1.x ; x--;}
};
int main( )
{
Someclass a(4);
Someclass b = a;
Someclass c(2);
c = a;
cout << "a.x = " << a.x << endl;
cout << "b.x = " << b.x << endl;
cout << "c.x = " << c.x << endl;
}
The question was not:
How many characters are in the array?
it was:
What will sizeof(<array name>)?
In the example program I gave, sizeof(a) returns the value 2 while sizeof(b) returns the value 10
Actually there is a global "operator new" that can be overridden on both a "per class" basis *or* globally overridden. It depends on whether the definition of the operator is within a class scope or the global scope.
The default behavior of new is defined in the C++ standard, and does not vary. However, it the behavior if the new operator is overloaded, there is no guarantee that initialization (or that anything at all will happen). The use of the new operator will be governed by, and limited to, whatever functionality is in the overloaded function definition.
It is true, you can't declare an uninitialized reference. However, Zaphod's modification of the constructor is correct. The the initialization list is the only place a member reference can be initialized.
- Wandering programmer February 28, 2010Brooks is right. As written, the code segment will compile. However, the function definition:
void Base::method() { n = 1;}
is ignored because the function method() in the base class is a pure virtual function. If one attempts to instantiate an object of either of the types Base or D1, the program will fail. The error will state that an abstract class cannot be instantiated.
Actually it depends on whether you use the function or not. This will compile with both statements because the local declaration shadows the scope of the external declaration. However, if the function is actually used, there has to be an actual definition of the function somewhere. Neither of these two statements provide such a definition.
- Wandering programmer February 28, 2010Um... actually the answer is:
b.x = 5
because in this case, the copy constructor is called. In no case could the result be 4 since "=" symbol forces either the copy constructor or the assignment operator to be invoked. The statements
Someclass c(2);
c = a;
will give the result 3, not 4 because the assignment operator will be called rather than either of the constructors.
nor do I...
It also compiles just fine on my system.
Actually I was quite surprised at the answer I got for this one. I would have thought that the value returned would be the size of (char *) the pointer. However, I compiled the following program:
#include <stdio.h>
main () {
char a[] = " ";
char b[10];
printf ("Sizeof(a) = %d\n", sizeof(a) );
printf ("Sizeof(b) = %d\n", sizeof(b) );
}
The resulting output was as follows:
Sizeof(a) = 2
Sizeof(b) = 10
What can I say but... go figure
RepHi, I’m Jamie from the Portsmouth USA and I am working as an account manager. I work for a ...
- Wandering programmer September 12, 2018