Browsing the archives for the Programming tag

Sub-Array with the Largest Sum

Tweet Question: You are given an array with integers (both positive and negative) in any random order. Find the sub-array with the largest sum. Answer: This is an all-time favorite software interview question. The best way to solve this puzzle is to use Kadane’s algorithm which runs in O(n) time. The idea is to keep […]

Little Endian or Big Endian?

Tweet Question: Is your machine little or big endian? Answer: Let first take a look at the solution before we discuss about it. int isLittleEndian( void ) { unsigned int temp = 0×12345678; char * tempAddress = &temp; if( *tempAddress == 0×12 ) { return 0; // Indicating False } else { return 1; // […]

2 Comments

Reverse the Order of Words in a String

Tweet Question: Reverse the order of words in a string in C using as little additional memory as possible. Answer: The solution for this questions uses the solution of Reverse a String. I am going to make a small change to that solution by adding a length parameter. It will probably be better for you […]

Reverse a String

Tweet Question: Reverse a string in C using as little additional memory as possible. Answer: The first solution needs the┬ásize of a char and size of two integers, all of which will be allocated from the stack. This solution is the most commonly accepted “good” solution. Here is the code. void reverseString(char* str) { int […]

Power of 2

Tweet Question: How do you find out if an unsigned integer is a power of 2? Answer: The trick here is to know that an unsigned integer which is a power of 2 has only one of its bits as 1. So a simple solution would be to loop through the bits and count the […]