Intel Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
1
of 1 vote

Here is my code:

#include <stdio.h>
char* strFind(char*pSrc, char*pSub)
{
  char  *pSrcTemp, *pSubTemp;
  if(NULL==pSrc||NULL==pSub)
    return NULL;
  if(!*pSub)
    return pSrc;
  for(pSrcTemp=pSrc, pSubTemp=pSub;*pSrcTemp;)
  {
    if(*pSrcTemp==*pSubTemp)
    {
      pSrcTemp++;
      pSubTemp++;
      if(!*pSubTemp)
      {
        return(pSrcTemp-(pSubTemp-pSub));
      }
    }else
    {
      pSrcTemp=pSrcTemp-(pSubTemp-pSub)+1;
      pSubTemp=pSub;
    }
  }
  return NULL;
}

int main()
{
    char src[]="why should i love  programming? i love programming because it lets me not feel boring";
    char sub[]=" love p";
    printf("find str(%s) from str (%s) at (%s)\n", sub, src, strFind(src, sub));
}

- chenlc626 March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are a good programmer

- Anand March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Put the first string in HashTable or HashMap. compare the other string with elements in hashMap. If it matches.... Done.

or for loops inside another for loop.

Bigger for loop is equal to one
comapare the characters of each string ... if matches ... DONE

- Neo March 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since O(n^2) is fine, just iterate through the larger string comparing against smaller string. Each time a match gets broken reset the index counter to it's previous position + 1 and start matching again.

- Anonymous March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
import java.lang.*;
public class Firstocc {

/**
* @param args
*/
public static void main(String[] args)throws Exception {
String s="";
String ms=" ";
String sub="";
boolean b=false;
Scanner sc=new Scanner(System.in);
System.out.print("enter the actual string");
s=sc.next();
System.out.print("enter the searching string");
sub=sc.next();
int subl=sub.length();
System.out.print(subl);
int n=s.length()-subl;
for(int i=0;i<n;i++)
{
ms=s.substring(i,subl);
//System.out.println("matching string is"+ms);
if(sub.equalsIgnoreCase(ms))
{
b=true;
System.out.println("matching string first occurance position id:"+i);
exit(0);
}
subl++;
}
if(!b)
{
System.out.println("string doesn't match");
}
}

private static void exit(int i) {
System.exit(0);

}

}
sampe o/p:enter the actual stringhellokittuhwrukittu
enter the searching stringkittu
5matching string first occurance position id:5

- ram97 March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Typical usecase for Suffix Tree.

- Varun March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Easy Fast Solution we know that we can index an array by a char A['d'], So we can use the same approach by
Initialize an BOOLEAN array of 128 (ascii chars) to false;
i.e. Boolean [] asciiArray = new Boolean[128]
for(int i < occurence.length(); i++) {
asciiArray[occurence[i]]=true;
}
After iitialization, we can map this array to the string we want to find the occurence, resetting if the no match.

- Tremayne March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class MyString {

	public static void main(String []args) {
		String str = "ababcdef";
		String subStr= "abc";
		boolean flag = false;

		if (str.length() < subStr.length())
			System.out.println(subStr + " is not a sub string of  " + str);
		int i = 0 
		for ( ; i < str.length() -subStr.length() + 1; i++)  {
			int index = 0; 
			flag = true; 
			for ( int j = 0 ; j < subStr.length() ; j++) {
				
				if (str.charAt(i+index) != subStr.charAt(j)) {
					flag = false;
					break;
				}
				index++;
			}
			if (flag) break;
		}
		if (flag)	{
			System.out.println(subStr + " is a sub string of  " + str);
		} else	{
			System.out.println(subStr + " is not a sub string of  " + str + " @ index of  " + i);
		}
	}
}

- PCB March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a simple idea:
if s1< s2 where s1 and s2 are two strings..

int hashcode = s1.hashcode();
for(int i=0;i<s2.length-s1.length;i++)
{
// get the hashcode of the second substring with length of s1..
// and compare.. if it matches, we get first occurance..
// and return the index;
}

- jimmy514in March 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here we have to find the first occurance of one string in to another,
str1="abc"
str2="ahmedabad"
For this case, how many time 'a' occurs in str2 that we have to find.
Correct me if I am wrongly interpreting.

code:

char ch=str1.charAt(0);
int count=0;
for(i=0;i<str2.length();i++)
{
if(ch==str2.charAt(i))
count++;
else
continue;
}
Sytem.out.println("Occurance is : " + count + "times");


Does it not this much simple?

- Karimkhan March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It is not like that, We have to find whether abc is available in ahemadabad.....
if so, we have to find in which place abc is there in ahemadabad.......

- Anand March 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ #include<stdio.h> int main() { int n,cnt=0,startindex=0,endindex; char *p1,*p2; char str[]="KMIT Finishing School"; char substr[]="Finishing"; n=strlen(substr); endindex=n; p1=str; p2=substr; do { p1++; startindex++; } while(*p1!=*p2); while(*p1==*p2 && *p1!='\0' && *p2!='\0') { p2++; p1++; cnt++; } endindex+=startindex; if(cnt==n) { printf("Given string is sub string:"); printf("\n starting index:%d ending index:%d",startindex,(endindex-1)); } else printf("Given string is not a sub string:"); } - Vaibhav Agarwal March 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Doesn't work. Try
char str[] = "KMIT Finish Finishing School";

- Anonymous March 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

// returns index of the first matching char if sub string is found otherwise returns -1

int mystrstr(char *source , char*substr)
{
int i = 0, j = 0;
while (source[i] && substr[j])
{
if(source[i]==substr[j])
{
i++;
j++;
if(!substr[j])
return (i-j);
}
else
{
i=i-j+1;
j=0;
}

}

return -1;
}

- SDguy April 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Fastest linear solution

private static bool matchString(string s, string find)
        {
            int matchPtr = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i].Equals(find[matchPtr]))
                {
                    if (matchPtr + 1 == find.Length) return true;
                    matchPtr++;
                }
                else
                {
                    matchPtr = 0;
                }
            }
            return false;
        }
    }

- ZM April 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool sub_string(char* m_str, char* sub_str)
{
if (m_str == NULL || sub_str == NULL)
return false;
char* m_str_tmp = m_str;
char* sub_str_tmp = sub_str;
int index;
bool flag = true;
while (*m_str_tmp)
{
if (*m_str_tmp == *sub_str_tmp)
sub_str_tmp++;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
sub_str_tmp = sub_str;
if(*m_str_tmp != NULL && *sub_str_tmp == NULL)
return true;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
return false;
m_str_tmp++;
}

return false;
}

- David November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool sub_string(char* m_str, char* sub_str)
{
if (m_str == NULL || sub_str == NULL)
return false;
char* m_str_tmp = m_str;
char* sub_str_tmp = sub_str;
int index;
while (*m_str_tmp)
{
if (*m_str_tmp == *sub_str_tmp)
sub_str_tmp++;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
sub_str_tmp = sub_str;
if(*m_str_tmp != NULL && *sub_str_tmp == NULL)
return true;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
return false;
m_str_tmp++;
}

return false;
}

- David November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void SearchOne(String searchString, String OriginalString){
String temp="";
for(int i=0;i<OriginalString.length()-1;i++){
char c=OriginalString.charAt(i);
temp=""+c;
for(int j=i+1;j<OriginalString.length();j++){
temp=temp+OriginalString.charAt(j);
System.out.println("Temp is "+temp);
if(temp.equalsIgnoreCase(searchString)){
System.out.println("Found at "+i+"th and "+j +"th iteration");
return;
}
}
}
}

- Akhil Kumar August 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void SearchOne(String searchString, String OriginalString){
String temp="";
for(int i=0;i<OriginalString.length()-1;i++){
char c=OriginalString.charAt(i);
temp=""+c;
for(int j=i+1;j<OriginalString.length();j++){
temp=temp+OriginalString.charAt(j);
System.out.println("Temp is "+temp);
if(temp.equalsIgnoreCase(searchString)){
System.out.println("Found at "+i+"th and "+j +"th iteration");
return;
}
}
}
}

- Akhil Kumar August 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C- Solution
// Stringoccurence.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <Windows.h>
using namespace std;

bool occurence(char* str1, char* str2)
{
int sizeStr2 = 0;
char* sample = str2;
bool bFound = false;
int count = 0;
//Compare str1and str2, increment str2 only if a match is found
while (*sample != '\0')
{
++sizeStr2;
++sample;
}
sample = str2;

while (*str1 != 0)
{
if (*str1 == *str2)
{
++str1;
++str2;
++count;
if (count == sizeStr2)
{
bFound = true;
return bFound;
}
}
else
{
++str1;
str2 = sample;
}
}

return bFound;
}


int _tmain(int argc, _TCHAR* argv[])
{
char* str1 = "Sampletesttest";
char *str2 = "test";
bool bFound = occurence(str1, str2);
cout << bFound;
_getch();
return 0;
}

- Siddharth More September 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>

using namespace std;

int strFind(string str,string str2)
{
//assume str.length()> str2.length()

for(int i=0;i<str.length();i++)
{
for(int j=0;true;j++)
{
if(str[i]==str2[j]){
i++;
}else{break;}
if(str2[j+1]=='\0')
{
return i-j-1;
}

}
}
return -1;
}
int main()
{
string src="why should i love programming? i love programming because it lets me not feel boring";
string sub=" love p";
cout<<"find str("<<sub<<") from str ("<<src<<") at "<< strFind(src, sub)<<endl;
}

- Jignesh patel July 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

In case of c or c++, we can use built in function strstr which will return the first occureence of substr in srcstr................

#include<iostream.h>
#include<string.h>
void main()
{
char src[]="why should i love programming? i love programming because it lets me not feel boring";
char sub[]=" love p";
char *p=strstr(src,sub);
if(p)
cout<<p;
else
cout<<"The string is not present";
}

- Anand March 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Writing void main, that would be enough for me to say "Thank you. There is the door."

- Anonymous March 07, 2013 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More