language-agnostic

linear simulation of multidimensional array

I know how to simulate a 2d array in a linear array using [x + y * width] as a linear index. I can extend this to 3d arrays: [x + y * width + z * width * height]. Is there a general formula for N-dimensional array? I'm looking for a language-agnostic answer. ...

What should I really name the variable which represents the sum of a vector's components?

Is there a valid math term? I could just name this guy "sumXY", but that is (a) lame, and (b) not scalable, since going up a dimension would require a rename. While typing I thought of "componentSum", but I'd love to know if there's a real name for it. ...

What is the difference between IF-ELSE and SWITCH?

Can someone please explain this to me? ...

Software solution for communication between two teams?

My department is working on a project that requires us to heavily communicate with another team, whose software ours has to interact with. Today this resulted in a 4 1/2 hour conference call where the whole team had to attend, with very few results and no progress in the actual coding at all. To me, it was a complete waste of time, exce...

Reader Writer Problem

Sorry if I am asking same question again but want to verify! I have two processes P1 and P2. P1 is a writer (Producer). P2 is a reader (Consumer). There is some shared memory or a file that P1 writes to and as soon as P1 writes, P2 should be notified for reading. Now as per my understanding pseudocode for P1 should be Open shared ...

Arrange 0's & 1's in a array

This is one of an interview question which I had recently. I would like to know others perception of approach for this problem. Question: You are given a structure which holds an employee details with two elements as int Deptartment and string Name. struct Employee { string Name; int Dept; } You are given details of N Emplo...

What will happen if function goes out of stack space during infinite recursion?

There is a function that calls itself recursively infinitely. This function has some arguments too. For every function call the arguments and return address are pushed on the stack . For each process there is fixed size of stack space that cannot grow dynamically like heap. And I guess each thread also has its own stack. Now if a fun...

Design a networking application

Problem: I need to design a networking application that can handle network failures and switch to another network in case of failure. Suppose I am using three ethernet connections and one wireless also . At a particular moment only one connection is being used. How should I design my system so that it can switch to another network in ...

design a stack such that getMinimum( ) should be O(1)

This is one of an interview question. You need to design a stack which holds an integer value such that getMinimum() function should return the minimum element in the stack. For example: consider the below example case #1 5 --> TOP 1 4 6 2 When getMinimum() is called it should return 1, which is the minimum element in the stack. ...

Implementing run-length encoding

I've written a program to perform run length encoding. In typical scenario if the text is AAAAAABBCDEEEEGGHJ run length encoding will make it A6B2C1D1E4G2H1J1 but it was adding extra 1 for each non repeating character. Since i'm compressing BMP files with it, i went with an idea of placing a marker "$" to signify the occurance of a...

Algorithm to find a square root of a number?

The only algorithm that I know for this problem is the Newton's method (make a guess, then improve it until it's good enough). Any other ideas (use any language you prefer)? PS: Of course I don't have any use case for this, I'm just researching it for academic reasons. ...

Shutdown a remote computer connected in LAN in any preferable language

I want to shut down the remote computer (ex. a computer connected in LAN.) I have tried a lot but I could not find the solution. Does anyone have an idea on how to shut down the remote computer? Any language would work. ...

Validating string lengths based on related database columns

At the one end of my web application I have a database table storing a load of pieces of text. In the middle I have an API that separates my application tiers. At the other end I have a user interface consisting of many TextBoxes (or input type=text form elements, if you prefer). I need the maxlength properties of the TextBoxes to be ...

What websites do you use to post temporary code snippets?

Following on the heels of What is your preferred site for code snippets, I'm looking for a website to post a short blurb of formatted code. I don't want to create a login. I'm looking for a simple: paste, use and forget. I know it exists, I've used it before, but my google skills are failing me! ...

Converting RGB to grayscale/intensity

When converting from RGB to grayscale, it is said that specific weights to channels R, G, and B ought to be applied. These weights are: 0.2989, 0.5870, 0.1140. It is said that the reason for this is different human perception/sensibility towards these three colors. Sometimes it is also said these are the values used to compute NTSC sign...

confused about spin lock

I read spin lock code from here, especially this part inline void Enter(void) { int prev_s; do { prev_s = TestAndSet(&m_s, 0); if (m_s == 0 && prev_s == 1) { break; } // reluinquish current timeslice (can only // be used when OS available and // we do NOT wa...

What is the maximum number of weekdays in a year? How would you code it?

In order to allocate enough space for an array element for each weekday of a year, I'm trying to work out the maximum number of rows I would need. That lead me onto the question of how I would work it out. Would you have to calculate the number of days in every year for the next n years and go with that? Or is there (as I suspect) a mo...

Source code viewers to display parts of the same file side by side

Are there any tools or IDE features to allow viewing of different parts of the same file side-by-side. I've often thought this would be useful for analysing and refactoring duplicated code within a file. ...

Find the mode of an array of floating point numbers using hashing

I recently read that a method involving hashing could be a good way to find the mode of an array of floating point numbers. I don't know much about hashing or its applications and the text didn't explain further. Does anyone know what this method would involve? ...

Realistic time estimates for progress bars etc.

I know I am not the only one who does not like progress bars or time estimates which give unrealistic estimates in software. Best examples are installers which jump from 0% to 90% in 10 seconds and then take an hour to complete the final 10%. Most of the time programmers just estimate the steps to complete a task and then display curren...