-1
\$\begingroup\$

Two Sum Problem:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Code:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target){
    int *a = malloc(sizeof(int) * 2);
    for(int i = 0; i < numsSize; i++){
        for(int j = i+1; j < numsSize; j++){
            if (nums[i] + nums[j] == target){
                a[0] = i;
                a[1] = j;
                return a;
            }
        }
    }
} /* Error: control reaches end of non-void function [-Werror=return-type] */
\$\endgroup\$

closed as off-topic by 200_success, Toby Speight, Stephen Rauch, Sᴀᴍ Onᴇᴌᴀ, Dannnno Apr 13 '18 at 15:54

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, Toby Speight, Stephen Rauch, Sᴀᴍ Onᴇᴌᴀ, Dannnno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Are you asking about the error? (your title suggest the same). The error here is quite straight forward: If nums simply do not contain a combination that adds to target, then the method lacks a return statement. You could e.g. return -1 twice if no combination is found. However, this is not on topic for CodeReview.SE since this code currently does not work. StackOverflow would be the place to post this. \$\endgroup\$ – Flater Apr 13 '18 at 10:04
0
\$\begingroup\$

I would have done this:

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
#define INVALID (-1)
int* twoSum(int* nums, int numsSize, int target)
{
    int *result = malloc(sizeof(int) * 2);
    memset (result, INVALID, sizeof(int) * 2);
    for(int left = 0; left < numsSize && result[0] == INVALID; left++)
    {
        for(int right = left+ 1; right < numsSize && result[0] == INVALID; right++)
        {
            if (nums[left] + nums[right] == target)
            {
                result[0] = left;
                result[1] = right;
                /* A return statement here instead of the overhead of the 
                   second check in the for loop conditions may be a good
                   idea, it depends if the driving factor is performance
                   or maintainability */
            }
        }
    }
    return result;
} 

Since this is an interview question and you are showing what a shining star you are you might want to consider the following points:

  • Its best to avoid multiple return points if you can, but there is going to be a performance hit. By adding the comment I think I have shown that I have identified that issue and, right or wrong, made a decision and explained why.

  • Setting the results as early as possible may be a waste of a few processor cycles but it will help when the code goes wrong, you know you are going to have random values sneaking out, imagine what would happen if results[0] = 5;

  • Using single letter variables means you have to right more comments/documents to explain your code. This is an interview question, you don't want to submit a 20 page document detailing what a, i, j mean and their range of values do you. (left and right are not very descriptive names, but they are better than i and j).

All I did was add one line and polish the turd a bit, which is much easier when I'm not being interviewed, good luck !

\$\endgroup\$
  • 1
    \$\begingroup\$ @pacmaninbw - Thanks, copy and paste error, now fixed. \$\endgroup\$ – Code Gorilla Apr 13 '18 at 14:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.