Skip to main content

Posts

Showing posts from June, 2008

Write a function that reverses the order of the words in a string.( Words not the individual charecterS)

For example, your function should transform the string “Do or do not, there is no try.” to “try. no is there not, do or Do”. Assume that all words are space delimited and treat punctuation the same as letters. Just for variety, solve this problem in C, not Java or C#, and assume that you’re only dealing with ASCII characters that can be safely stored in byte arrays. You probably already have a pretty good idea how you’re going to start this problem. Because you need to operate on words, you have to be able to recognize where words start and end. You can do this with a simple token scanner that iterates through each character of the string. Based on the definition given in the problem statement, your scanner will differentiate between nonword characters - namely, the space character - and word characters , which for this problem are all characters except space. A word begins, not surprisingly, with a word character and ends at the next nonword character or the end of

Remove Specified Characters from string.

Because this problem involves string manipulation and the string class is immutable, you’ll need to either convert the string to a character array or use a StringBuilder for your manipulations. Using an array will save you a lot of overhead, so that’s what you should use. This problem breaks down into two separate tasks. For each character in str , you must determine whether it should be deleted. Then, if appropriate, you must delete the character. The second task, deletion, is discussed first. Your initial task is to delete an element from an array. An array is a contiguous block of memory, so you can’t simply remove an element from the middle as you might with a linked list. Instead, you’ll have to rearrange the data in the array so it remains a contiguous sequence of characters after the deletion. For example, if you want to delete “c” from the string “abcd” you could either shift “a” and “b” forward one position (toward the end) or shift “d” back one position (towar

Write an efficient function to find the first nonrepeated character in a string.

At first, this task seems almost trivial. If a character is repeated, it must appear in at least two places in the string. Therefore, you can determine whether a particular character is repeated by comparing it with all other characters in the string. It’s a simple matter to perform this search for each character in the string, starting with the first. When you find a character that has no match elsewhere in the string you’ve found the first nonrepeated character. What’s the time order of this solution? If the string is n characters long, then in the worst case you’ll make almost n comparisons for each of the n characters. That gives worst case O( n 2 ) for this algorithm. You are unlikely to encounter the worst case for single-word strings, but for longer strings, such as a paragraph of text, it’s likely that most characters would be repeated, and the most common case might be close to the worst case. The ease with which you arrived at this solution suggests that there

Write a function that determines the number of 1 bits in the computer’s internal representation of a given integer

This problem may at first sound like a base conversion problem in which you have to design an algorithm to convert a base 10 number to a two’s complement binary number. That approach is circuitous because the computer already internally stores its numbers in two’s complement binary. Instead of doing a base conversion, try counting the 1’s directly. You can count the number of 1’s by checking the value of each bit. Ideally, you’d like to use an operator that would tell you the value of a specified bit. That way, you could iterate over all of the bits and count how many of them were 1’s. Unfortunately, this ideal operator doesn’t exist. You can begin by trying to create a procedure that determines the value of each bit using the existing bit operators. Focus on figuring out a way to get the value of the lowest bit. One way to do this is to AND the given integer with the value 1. Let’s use 8-bit integers to keep our examples manageable, in which case 1 is stored as 00000001.

Big-endian or Little-endian :Important Write a function that determines whether a computer is big-endian or little-endian.

Big-endian or Little-endian: Important Write a function that determines whether a computer is big-endian or little-endian. This problem tests your knowledge of computer architectures as much as it tests your ability to program. The interviewer wants to know whether you are familiar with the term endian. If you are familiar with it, you should define it or at least try to point out the differences between big-endian and little-endian, even if you forget which is which. If you are not familiar with the term, you’ll have to ask the interviewer to explain it. Endianness refers to the order in which a computer stores the bytes of a multibyte value. (Or, technically, the units of a multiunit value - for example, the computer may use a 16-bit unit size instead of an 8-bit unit size. We restrict ourselves to 8-bit units for simplicity, however.) Almost all modern computers use multibyte sequences to represent certain primitive data types. For example, an integer is usually 4 bytes. The by

Explain the difference between an interface and an abstract class in objectoriented programming

Explain the difference between an interface and an abstract class in object oriented programming. Answer: The specific answer to this depends on which language you’re developing with. An interface declares a set of related methods, outside of any class. An abstract class is an incomplete class definition that declares but does not define all of its methods. An interface defines an application programming interface (API) that is independent of any class hierarchy. In fact, interfaces can be used in non-OO programming models, such as component based models like COM and CORBA. However, you’re focusing on the use of interfaces in an object-oriented context, where they are useful in their own right. Interfaces are the ultimate encapsulators, because they hide all the details of the classes that implement their methods from the user of the interface. They’re particularly important - almost necessary, in fact - in languages that only support single inheritance (classes can only inherit from o

What is the difference between list and vector?

STL Question: What is the difference between list and vector? Answer: list and vector implements fundamentally different data structures. The major difference is the way of element access. Vector elements may accessed randomly while list elements must be accessed sequentially. Note that vectors are dynamic array, they can grow in size just like lists. But for in between insertions elements needs to be moved along with memory allocation and swapping. Second is the frequent insertion and deletion of elements is very expensive in case of vectors while it is efficient in case of vectors. Iterators are different as a result of the first item: list support bi-directional iterators while vector uses random access iterator. Some methods are difference (for example, only list supports merge and splice, adds push_front and pop_front etc.

What's the difference between aggregation and containment?

What's the difference between aggregation and containment? Answer: UML Question. It was quite interesting story, because I wrote the difference in terms of COM and the interviewer meant C++. The only problem that when he drew the question, it turned out that he hardly knows terminology himself. Trust me, in terms of C++ (to be precise in terms of UML) there is NO difference between aggregation and containment. Aggregation IS containment and containment IS aggregation. There is a difference, however, between aggregation and composition. This question should sound: "What's the difference between aggregation and composition in terms of UML?". Back to answer. The difference between aggregation and composition is life term of participating objects. Aggregated object can exist without container. Composited object is managed by it's container and cannot live without it. Interviewer would probably expect that you will say that composition imp

Calculating the Size of an Incomplete Array An incomplete array declaration can appear in a function's parameter list.

Calculating the Size of an Incomplete Array An incomplete array declaration can appear in a function's parameter list. For example: int count(const char s[]); The declaration of s doesn't include the array's size. Can count() use the operator sizeof to calculate the size of s? No, it can't. The compiler implicitly transforms an array into a pointer. Therefore, sizeof returns a pointer's size, not an array's size. One way to obtain the array's size is to pass an additional argument that holds the number of elements in the array: int count(const char s[], int arr_size); However, this solution is error prone. A better solution is to pass a vector and call its size() member function: int count(const vector &s ) { return s.size(); }

What is the size of an Empty Class, Class with virtual functions and class with only normal functions, no data members?

Size of an Empty Class. class MyClass { }; MyClass myObject; sizeof(myObject); The size of an empty class is 1 byte. Since size of class is calculated as the size of data members it contains. Since empty class doesnt contains any data member, its size is zero. but to keep the existence on object in memory size of empty class object is 1 byte. Size of Class with virtual functions class MyClass { public: virtual void myfunct(); }; MyClass myObject; sizeof(myObject); This will be 4 bytes. as this object will contain VPTR pointer to virtual table. As it has some size compile doesnt add one byte which it adds to the empty class. Size of class with only normal functions, no data members? class MyClass { public: void myfunct(); }; MyClass myObject; sizeof(myObject); This will again 1 byte as it has no data members. Class size is comprised of only data members and not member functions.

How to Compile and build Visual Studio (Visual C++) Projects on the Command Line

Compiling on the Command Line The project management and building of projects in Visual C++ IDE is very usefull and easy to use. Sometimes we might need to build projects from the command line,it might be more usefull in such casess.Visual C++ project files are very similar to carefully formatted scripts for the Nmake utility, Microsoft's version of the standard Make program, but if you need to compile from the command line, you will need to export the .mak file. If you're going to directly modify a Visual C++ produced .mak file, you should be careful about how you do it, but if you do make a mistake, you can always go back to the .dsw file and use it to export a new makefile. Once you've properly set up the environment to include the location of the tools in your PATH= environment variable, the libraries in your LIB= environment variable, and the headers in your INCLUDE= environment variable, you can run a build for a project named Sample by making the project's direc

Explain Customized control development? What is subclassing ?

When we are putting one control on any dialog, we are handling its messages or events in its parent (classes). We use functionalilty and interface provided by that control only. We cannot add our own fuctionality as per our need while handling messages in parent. Let us take up a scenario when we want an edit control which should accept only letters, digit and specific symbols. The default functionality will allow on datatypes and minimum maximum values and range validation using DDV(Dynamic Data validation ). If you are not using Dynamic Data validation then you can validate the edit control values only when you are clicking/hitting return key at list once in control or parent dialog. To validate the values entered in edit control at runtime we nee to customize the control. This is also called as subclassing, its similar to API Hooking. Windows Subclassing- usually when and application runs it seeks for messages for its windows. if any messages comes that is handled in the Windows p

How to debug an exe( application) which is launched from another exe(application) ?

1. Set the MessageBox in the another exe( source code)which you want to debug, at the location (function where you want to debug) or at initinstance if you want to debug from beginning. Compile the code and build the exe. 2. Launce Maina Exe (first application). 3. Launch second exe from first exe. 4. Second exe will halt at the MessageBox location till user is not clicking ok button. 5. now open Visual Studio editor. select Debug->Processes, from process select your second exe name. and click attach process. 6. Now second application will be in debugging mode and hault at messageBox. 7. Now put the break point after the MessageBox code in second exe. 8. Click ok button on MessageBox. 9. Debugger will halt at the break point placed after the MessageBox code. now you can debug your second exe(application) here onwards.

What is Serialization ? Explain in detail.

Its a process of storing objects to the disk( persistent ) storage and retrieving stored objects from disk storage. To add this functionality to a class 1. That class must be derived from CObject. 2. Need to add two macros DECLARE_SERIAL and IMPLEMENT_SERIAL. 1. Once inside the class declaration and another outside the class. 2. Following arguments we need to pass to the IMPLEMENT_SERIAL macro The class name, the base class name, and the version number 3. CArchive Class is used for storing and retrieving information. 1. By default CArchive class uses CFile to write objects. 2. By default it writes objects in Binary mode. 3. One can determine if CArchive is reading or writing the objects. by calling the IsStoring or IsLoading functions.

When is the OnIdle function called? how we can call it repeatedly? difference betn thread and onIdle() task ?

When the application is idle and there are no messages in the application message queue. Returning a value of TRUE will cause the OnIdle function to continue to be called as long as the application remains idle. An OnIdle task executes only when the application is idle and there are no messages in the message queue. A thread executes independently of the rest of the application.

What Is an ActiveX Control?

ActiveX controls are components that can be used to add new functionality to your application. ActiveX technologies are all built on top of the COM specification. In COM, an interface is a group of related functions that are implemented together. All interfaces are named beginning with I , such as IDataObject . An ActiveX control must be a COM object. This means that an ActiveX control must support IUnknown , the interface supported by all COM objects. This allows for a great deal of latitude when deciding how a control is to be implemented; Support for the IUnknown interface is provided automatically when you use MFC and ControlWizard to build your control. Reducing the number of required interfaces allows ActiveX controls to be much smaller than the older OLE controls. It also makes it feasible to use ActiveX controls to implement functionality where the size of the control is an important factor. Web pages can be more intelligent when a control is downloaded and activated to

What is a Function Pointer ?

Function Pointers are pointers, i.e. variables, which point to the address of a function. Executing a program gets a certain space in the main-memory. Both, the executable compiled program code and the used data variables, are put inside this memory. Thus a function in the program code is, like e.g. a character field, nothing else than an address. Syntax: // define a function pointer and initialize to NULL int (*pt2Function)(float, char, char) = NULL; // C int (MyClass::*pt2Member)(float, char, char) = NULL; // C++ int (MyClass::*pt2ConstMember)(float, char, char) const = NULL; // C++ To these pointers on can assign the address of functions matching with the similar signatures Function Pointers provide some extremely interesting, efficient and elegant programming techniques. You can use them to replace switch/if-statements, to realize your own late-binding or to implement callbacks. Due to their complicated syntax – they are treated quite stepmotherly in most computer books and do

Explain Splitter Window ?

Splitter window resides within the frame window. It is divided into several panes, each pane can have a different size. Splitter window provides the user with several different views for monitoring data contained in the document at the same time. Normally, the size of each pane can be adjusted freely, this gives the user a better view of data. There are two types of splitter windows: Dynamic Splitter Window and Static Splitter Window. For a dynamic splitter window, all views within the splitter window are of the same type. The user can create new panes or remove old panes on the fly. For a static splitter window, the views could be of different types and the number of panes has to be fixed at the beginning. In this case, the user can not add or delete views after the program has started. Both SDI and MDI applications can have splitter windows. In an SDI application, the splitter window is embedded in the mainframe window. In an MDI application, it is embedded in the child frame window.

What is duplicating handle ?

The parent and child processes may require different access to an object identified by a handle that the child inherits. A process may also need a real, inheritable process handle rather than the pseudo handle produced by GetCurrentProcess for use by a child process. To address this issue, the parent process can create a duplicate handle with the desired access and inheritability. Function to duplicate handles: BOOL DuplicateHandle ( HANDLE hSourceProcessHandle, HANDLE hSourceHandle, HANDLE hTargetProcessHandle, LPHANDLE lphTargetHandle, DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwOptions) Upon completion, lphTargetHandle points to a copy of the original handle, hSourceHandle . hSourceHandle is a handle in the process indicated by hSourceProcessHandle and must have PROCESS_DUP_HANDLE access; DuplicateHandle will fail if the source handle does not exist in the source process. The new handle, which is pointed to by lphTargetHandle , is valid in t