Saturday, 27 July 2019 17:05

## SORTing Algorithm – Interview Question

Written by https://sapyard.com/sorting-algorithm-interview-question-2/
Rate this item

In our SAP Technical Telegram Group, we discuss everything about SAP. UI5, OData, Fiori issue,

target="_blank" rel="noreferrer noopener" aria-label="New ABAP (opens in a new tab)" href="https://sapyard.com/tutorials-on-sap-abap-on-hana/" target="_blank">New ABAP syntax, CDS approaches, Performance Tuning debates. You think of a topic, we discuss it in our group. No wonder, we are one of the largest SAP Technical group with zero spamming.

Yesterday, one member asked the below question. It was no doubt an interview question from one of the largest IT Company in the world.

Question: How to write the entries of first and second table to third table without SORT statement?

No SORT statement should be there in the solution.

The moment this question was floated, there was a debate among the members asking what is the business requirement? Why can we not use SORT?

Then the member who asked this question revealed it is one of the interview question from one the Big Multi National IT Companies.

Then the next debate started that we should not ask such questions in interview. But one of the member defended saying that SORTing Algorithms are asked by interviewers to eliminate at the very first stage of the selection process.

Ok, leaving behind the debate, whether such interview questions actually test the programming skill or not, everyone agreed to find a solution.

We received 2 approaches with working code snippet.

## Solution 1 – Provided by Sercan Küçükdemirci

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 TYPES:tt_intTYPETABLEOFiWITHEMPTYKEY. DATA(it_one)=VALUEtt_int((1)(9)(12)(16)). DATA(it_two)=VALUEtt_int((4)(10)(15)(20)). DATA:it_threeTYPEtt_int. APPENDLINESOFit_oneTOit_three. APPENDLINESOFit_twoTOit_three. DATA(lv_len)=lines(it_three). DATA(i)=1. WHILEi<lv_len. DATA(lv_min)=it_three[i]. DATA(j)=i+1. WHILEj<lv_len+1. IFit_three[j]<lv_min. DATA(lv_temp)=lv_min. lv_min=it_three[j]. it_three[j]=lv_temp. ENDIF. j=j+1. ENDWHILE. it_three[i]=lv_min. i=i+1. ENDWHILE.

Team SAPYard validated the output. It is as per the requirement. Thank you Sercan!! You may connect with Sercan at LinkedIn.

## Solution 2 – Provided by Stephan Koester

 1 2 3 4 5 6 7 8 9 10 TYPES: gtyt_intergerTYPETABLEOFiWITHEMPTYKEY, gtyt_interger_sortedTYPESORTED TABLE OFiwithNON-UNIQUE KEYtable_line. DATA(lt_table1)=VALUEgtyt_interger((1)(9)(12)(16)). DATA(lt_table2)=VALUEgtyt_interger((4)(10)(15)(20)). DATA(lt_table3)=CORRESPONDINGgtyt_interger_sorted(lt_table1). INSERTLINESOFlt_table2INTOTABLElt_table3.

Check the output.

Just the INSERT statement did the Magic!! Amazing solution.

Also Read:My First Program in S/4HANA

Every developer think differently. And there can be different ways to achieve the same solution. Solution to this question is a perfect example.

Our SAP Technical Group is just an example of how we can collaborate and learn every day. No question is too silly to ask. Ask, Answer, Propose, Depend, Share and Learn is the motto of our SAP Group.

This is the exact code snippet and output shared by Stephan. All of us can keep this for our quick reference for new ABAP syntax and usage.

## Solution 3 – Provided by Stephan Koester

Stephan provided yet another solution without using INSERT statement. Check the below snippet.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 TYPES: gtyt_intergerTYPETABLEOFiWITHEMPTYKEY, gtyt_interger_sortedTYPESORTED TABLE OFiWITHNON-UNIQUE KEYtable_line. DATA(lt_table1)=VALUEgtyt_interger((1)(9)(12)(16)). DATA(lt_table2)=VALUEgtyt_interger((4)(10)(15)(20)). DATA(lt_table3)=CORRESPONDINGgtyt_interger_sorted(BASE(lt_table1)lt_table2). * Starting with NW7.51 It is possible to prevent duplicate entries using DISCARDING DUPLICATES * https://help.sap.com/doc/abapdocu_751_index_htm/7.51/en-US/index.htm cl_demo_output=>write_data(value=lt_table1name='First Table'). cl_demo_output=>write_data(value=lt_table2name='Second Table'). cl_demo_output=>write_data(value=lt_table3name='Third Table'). cl_demo_output=>display().

He has used the new BASE key word. BASE is a functional operand position in which a database convertible to the target type can be specified. BASE is use to specify a start value base for the new structure of internal table.

Also, we can use DISCARDING DUPLICATES keywords for handling duplicate rows in component operator CORRESPONDING. Check more about DISCARDING DUPLICATES in ABAP Help.

You may follow Stephan Koester at LinkedIn. You may also check Stephan’s website Koester Consulting for more SAP blogs.

Would you like to share some weird Interview Questions you faced? Anything.. Technical or Non-Technical.